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。