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。有什麼我失蹤?
我試圖用MockBean代替SpyBean,但仍然沒有改變。我還試圖將間諜報告移至@Test
方法而不是@Before
,並且仍然具有相同的NPE。