【发布时间】:2017-03-26 14:16:34
【问题描述】:
我有一个 Spring Boot 1.4.2 应用程序。在启动期间使用的一些代码如下所示:
@Component
class SystemTypeDetector{
public enum SystemType{ TYPE_A, TYPE_B, TYPE_C }
public SystemType getSystemType(){ return ... }
}
@Component
public class SomeOtherComponent{
@Autowired
private SystemTypeDetector systemTypeDetector;
@PostConstruct
public void startup(){
switch(systemTypeDetector.getSystemType()){ // <-- NPE here in test
case TYPE_A: ...
case TYPE_B: ...
case TYPE_C: ...
}
}
}
有一个组件决定了系统类型。该组件在从其他组件启动期间使用。在生产中一切正常。
现在我想使用 Spring 1.4 的 @MockBean 添加一些集成测试。
测试看起来像这样:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyWebApplication.class, webEnvironment = RANDOM_PORT)
public class IntegrationTestNrOne {
@MockBean
private SystemTypeDetector systemTypeDetectorMock;
@Before
public void initMock(){
Mockito.when(systemTypeDetectorMock.getSystemType()).thenReturn(TYPE_C);
}
@Test
public void testNrOne(){
// ...
}
}
基本上模拟工作正常。我的 systemTypeDetectorMock 被使用,如果我调用 getSystemType -> TYPE_C 被返回。
问题是应用程序没有启动。目前弹簧的工作顺序似乎是:
- 创建所有 Mocks(无需配置所有方法都返回 null)
- 启动应用程序
- 调用@Before-methods(将在其中配置模拟)
- 开始测试
我的问题是应用程序以未初始化的模拟开始。所以对getSystemType() 的调用返回null。
我的问题是:如何在应用程序启动之前配置模拟?
编辑:如果有人遇到同样的问题,一个解决方法是使用@MockBean(answer = CALLS_REAL_METHODS)。这调用了真正的组件,在我的情况下,系统启动了。启动后,我可以更改模拟行为。
【问题讨论】:
-
您可以手动注入模拟和调用初始化代码,如本答案所述:stackoverflow.com/a/31587946/3440376
标签: java spring spring-boot mocking integration-testing