Spring 使用模拟 Services/Components 启动集成测试
Spring Boot Integration Testing with mocked Services/Components
情况和问题:在Spring Boot中,如何将一个或多个模拟classes/beans注入应用程序以进行集成测试? Whosebug 上有一些答案,但它们主要针对 Spring Boot 1.4 之前的情况,或者对我不起作用。
背景是,在下面的代码中,Settings 的实现依赖于第三方服务器和其他外部系统。设置的功能已经在单元测试中进行了测试,因此对于完整的集成测试,我想模拟出对这些服务器或系统的依赖性,只提供虚拟值。
MockBean 将忽略所有现有的 bean 定义并提供一个虚拟对象,但此对象不提供注入此 class 的其他 classes 中的方法行为。使用@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);
}
}
并向应该使用模拟 class 但未收到此模拟 class 的服务发出吼叫:
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;
}
}
当您使用@MockBean 注释字段时,spring 将创建注释的模拟 class 并使用它自动装配应用程序上下文的所有 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);
}
看起来您在指定模拟行为之前正在使用 Settings 对象。
你必须 运行
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
配置设置期间。为防止这种情况,您可以创建特殊配置 class 仅供测试。
@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
情况和问题:在Spring Boot中,如何将一个或多个模拟classes/beans注入应用程序以进行集成测试? Whosebug 上有一些答案,但它们主要针对 Spring Boot 1.4 之前的情况,或者对我不起作用。
背景是,在下面的代码中,Settings 的实现依赖于第三方服务器和其他外部系统。设置的功能已经在单元测试中进行了测试,因此对于完整的集成测试,我想模拟出对这些服务器或系统的依赖性,只提供虚拟值。
MockBean 将忽略所有现有的 bean 定义并提供一个虚拟对象,但此对象不提供注入此 class 的其他 classes 中的方法行为。使用@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);
}
}
并向应该使用模拟 class 但未收到此模拟 class 的服务发出吼叫:
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;
}
}
当您使用@MockBean 注释字段时,spring 将创建注释的模拟 class 并使用它自动装配应用程序上下文的所有 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);
}
看起来您在指定模拟行为之前正在使用 Settings 对象。 你必须 运行
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
配置设置期间。为防止这种情况,您可以创建特殊配置 class 仅供测试。
@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