2016-11-26 5 views
1

저는 최근에 v1.3.3.RELEASE에서 v1.4.2.RELEASE로 업그레이드 한 스프링 부트 응용 프로그램이 있습니다.spring-boot-test spyBean 주석이 종속성을 주입하지 않습니다.

v1.3.3의 통합 테스트에서 나는 성공적으로 스파이 할 수있는 bean을 가지고 있습니다. 응용 프로그램 대신 test 및 그 이하의 passwordEncoder 프로파일을 사용하여 테스트를 실행했습니다.

@Bean 
    @Profile({"test"}) 
    PasswordEncoder passwordEncoder(){ 
     PasswordEncoder passwordEncoder = new  BCryptPasswordEncoder(ApplicationConstants.CRYPT_LOG_ROUNDS); 
     final String pwdHash = "$$CONST_HASH$$";   
     PasswordEncoder peSpy = spy(passwordEncoder); 
     Mockito.when(peSpy.encode("TEST_USER_CRED")).thenReturn(pwdHash); 
     return peSpy; 
    } 

나는 v1.4.2.RELEASE로 업그레이드를하고있는 중이 야하고 하나의 방법을 조롱하는 spyBean 주석을 사용하여 프로파일에 의존하지 싶습니다.

나는 그것을 밖으로 시도 내 시험 방법에 다음을 변경 한 - 내가 위를하려고 할 때

@RunWith(SpringRunner.class) 
    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class,DbUnitTestExecutionListener.class }) 
    @DbUnitConfiguration(dataSetLoader = ReplacementDataSetLoader.class) 
    @SpringBootTest(classes = Application.class,webEnvironment=WebEnvironment.RANDOM_PORT) 
    public class MockTests { 

     @SpyBean 
     PasswordEncoder passwordEncoder; 

     private MockMvc mockMvc; 

     @Before 
     public void setup() throws Exception { 
      this.mockMvc = webAppContextSetup(webApplicationContext) 
        .apply(springSecurity()) 
        .build(); 
      final String pwdHash = "$$CONST_HASH$$"; 
      Mockito.when(peSpy.encode("TEST_USER_CRED")).thenReturn(pwdHash); 
     } 

    } 

그러나, 나는 Mockito.when 문에서 NPE를 얻을. 내가 누락 된 것이 있습니까?

SpyBean 대신 MockBean을 사용하려고했지만 여전히 변경 사항이 없습니다. 나는 또한 @Before 대신에 메쏘드로 간첩 진술을 옮기려고했지만 여전히 같은 NPE를 가지고있다.

답변

0

문제는 TestExecutionListeners 주석과 함께 발생했습니다. 기존 리스너 외에도 MockitoTestExecutionListener을 추가하면 모의/스파이 콩 주입이 수정되었습니다.