繁体   English   中英

spring-boot-test spyBean注释不注入依赖项

[英]spring-boot-test spyBean annotation not injecting dependency

我有一个spring boot应用程序,我最近从v1.3.3.RELEASE升级到v1.4.2.RELEASE。

对于我在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。

问题出在TestExecutionListeners注释上。 除了现有的侦听器之外,添加MockitoTestExecutionListener还修复了mock / spy bean的注入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM