0

我想測試我的spring項目。我需要在測試類中模擬假裝客戶端,但正常實現將自動裝配而不是模擬實現。這裏是我的代碼的頂部。 plz幫助。僞裝客戶端的模擬實現不會自動裝配

@Profile("test") 
@Primary 
@Component 
public class PushDevClientMock implements PushDevClient { 

//apiKey is needed for comminucating with GCM 
private static final String apiKey = "AIzaSyC7nH1yMgfGnEHbOHCgAeOrspMReaj0hCg"; 
@Override 
public ResponseEntity<PushInstanceResponse> getPushInstance(@PathVariable("instanceId") String instanceId) { 

    ResponseEntity<PushInstanceResponse> response; 
    if (instanceId == null) { 
     response = new ResponseEntity<PushInstanceResponse>(HttpStatus.BAD_REQUEST); 
    } 
    else { 
     PushInstanceResponse body = new PushInstanceResponse(); 
     body.setApiKey(apiKey); 
     response = new ResponseEntity<PushInstanceResponse>(body,HttpStatus.ACCEPTED); 
    } 

    return response; 
} 

}

這裏是我的測試類:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebIntegrationTest({"server.port:0", "spring.profiles.active:test"}) 
public class RegistrationControllerTest { 

@Autowired 
WebApplicationContext wac; 

@Autowired 
TokenRepository tokenRepository; 

private static MockMvc mockMvc; 
private static ObjectMapper mapper = new ObjectMapper(); 
// token and sender id are required for communicating with google 
private static String senderId = "553921561995"; 
private static String token = 
"ecdNq6_jeTM:APA91bEgWsJeIS5cXFwWrj_83EKeLWRFf1" + 
     "-  lNQGXA1uWdzrfHLpd7fAY7ur6Pplc4TQuKmEDiSUhUBhDdQLwG2a_fxdgoGbDrKNLjPm2E7JOMJFjk65jtFGHrjJ39NkgABtfn6MDVUCQ"; 

@Before 
public void setup() { 
    this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).apply(springSecurity()).build(); 
    tokenRepository.deleteAll(); 
} 

回答

0

標註RegistrationControllerTest@ActiveProfiles("test")

0
  1. 我刪除@EnableFeignClients從Application.java
  2. 添加空的配置類是這樣的:

    @Configuration 
    @Profile("!test") 
    @EnableFeignClients(basePackages = "ir.pegahtech.backtory.push_api") //this is my project root 
    public class FeignClientConfiguration { 
    } 
    

現在一切工作正常