[英]Spring Boot Integration Testing with mocked Services/Components
情况和问题:在 Spring Boot 中,如何将一个或多个模拟类/bean 注入应用程序以进行集成测试? StackOverflow 上有一些答案,但他们专注于 Spring Boot 1.4 之前的情况,或者只是不适合我。
背景是,在下面的代码中,设置的实现依赖于第三方服务器和其他外部系统。 设置的功能已经在单元测试中进行了测试,因此对于完整的集成测试,我想模拟对这些服务器或系统的依赖,并只提供虚拟值。
MockBean 将忽略所有现有的 bean 定义并提供一个虚拟对象,但该对象在注入该类的其他类中不提供方法行为。 在测试之前使用@Before 方式设置行为不会影响注入的对象或不会注入其他应用程序服务(如 AuthenticationService)。
我的问题:如何将 bean 注入应用程序上下文? 我的测试:
package ch.swaechter.testapp;
import ch.swaechter.testapp.utils.settings.Settings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit4.SpringRunner;
@TestConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class})
public class MyApplicationTests {
@MockBean
private Settings settings;
@Before
public void before() {
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
}
@Test
public void contextLoads() {
String applicationsecret = settings.getApplicationSecret();
System.out.println("Application secret: " + applicationsecret);
}
}
下面是一个应该使用模拟类但不接收这个模拟类的服务:
package ch.swaechter.testapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
private final Settings settings;
@Autowired
public AuthenticationServiceImpl(Settings settings) {
this.settings = settings;
}
@Override
public boolean loginUser(String token) {
// Use the application secret to check the token signature
// But here settings.getApplicationSecret() will return null (Instead of Application Secret as specified in the mock)!
return false;
}
}
看起来您在指定其模拟行为之前正在使用 Settings 对象。 你必须跑
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
在配置设置期间。 为了防止这种情况,您可以仅为测试创建特殊的配置类。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class, MyApplicationTest.TestConfig.class})
public class MyApplicationTest {
private static final String SECRET = "Application Secret";
@TestConfiguration
public static class TestConfig {
@Bean
@Primary
public Settings settingsBean(){
Settings settings = Mockito.mock(Settings.class);
Mockito.when(settings.getApplicationSecret()).thenReturn(SECRET);
Mockito.doReturn(SECRET).when(settings).getApplicationSecret();
return settings;
}
}
.....
}
另外,我建议您使用下一个符号进行模拟:
Mockito.doReturn(SECRET).when(settings).getApplicationSecret();
它不会运行settings::getApplicationSecret
当您使用 @MockBean 注释字段时,spring 将创建注释类的模拟并使用它来自动装配应用程序上下文的所有 bean。
你不能创建与模拟自己
Settings settings = Mockito.mock(Settings.class);
这将创建第二个模拟,导致所描述的问题。
解决方案 :
@MockBean
private Settings settings;
@Before
public void before() {
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
}
@Test
public void contextLoads() {
String applicationsecret = settings.getApplicationSecret();
System.out.println("Application secret: " + applicationsecret);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.