Spring 引导单元测试运行整个程序
Spring Boot unit test runs the whole program
我想用 Spring Boot 实现集成测试。我从 spring-boot-starter-test
依赖项开始,版本 2.2.5.RELEASE
.
我有以下组件:
@Component
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
主程序如下所示:
@SpringBootApplication
public class Main implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Autowired
private MyMath myMath;
@Override
public void run(String... args) throws Exception {
System.out.println(myMath.add(2, 3));
}
}
它按预期工作 - 到目前为止,非常好。我想添加一个单元测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
这也有效,但根据日志,它执行了整个程序。我不想 运行 程序本身,只是 MyMath.add()
函数。我该怎么做?
到目前为止我尝试了以下方法:
@RunWith(SpringJUnit4ClassRunner.class)
提供了相同的结果。
- 省略
@SpringBootTest
结果 NoSuchBeanDefinitionException
。
- 重新格式化代码以使用 bean 而不是像下面这样的组件。
MyMath
无注释:
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
Main
保持不变。
@Configuration
public class AppConfig {
@Bean
public MyMath getMyMath() {
return new MyMath();
}
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
所以我不能做的是在没有 运行 整个程序的情况下测试组件。有什么可以帮助我的吗?谢谢!
您不需要重构您的代码。保持 MyMath class 原样
@Component
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
像这样更改你的测试class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyMath.class)
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
如果您的 MyMath class 自动装配了其他依赖项,这会变得有点复杂。那你就得用mocks了。
如果您的 MyMath class 如此简单,我不会使用 Spring 来初始化它。没有必要,更重要的是 - junit 应该很快。因此,您使用 Spring Context 进行的测试不是 运行,而是将其更改为简单的 JUnit 并像普通对象一样创建 MyMath:
public class MyMathTest {
private MyMath myMath = new MyMath();
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
我想用 Spring Boot 实现集成测试。我从 spring-boot-starter-test
依赖项开始,版本 2.2.5.RELEASE
.
我有以下组件:
@Component
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
主程序如下所示:
@SpringBootApplication
public class Main implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Autowired
private MyMath myMath;
@Override
public void run(String... args) throws Exception {
System.out.println(myMath.add(2, 3));
}
}
它按预期工作 - 到目前为止,非常好。我想添加一个单元测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
这也有效,但根据日志,它执行了整个程序。我不想 运行 程序本身,只是 MyMath.add()
函数。我该怎么做?
到目前为止我尝试了以下方法:
@RunWith(SpringJUnit4ClassRunner.class)
提供了相同的结果。- 省略
@SpringBootTest
结果NoSuchBeanDefinitionException
。 - 重新格式化代码以使用 bean 而不是像下面这样的组件。
MyMath
无注释:
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
Main
保持不变。
@Configuration
public class AppConfig {
@Bean
public MyMath getMyMath() {
return new MyMath();
}
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
所以我不能做的是在没有 运行 整个程序的情况下测试组件。有什么可以帮助我的吗?谢谢!
您不需要重构您的代码。保持 MyMath class 原样
@Component
public class MyMath {
public int add(int a, int b) {
return a + b;
}
}
像这样更改你的测试class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyMath.class)
public class MyMathTest {
@Autowired
private MyMath myMath;
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}
如果您的 MyMath class 自动装配了其他依赖项,这会变得有点复杂。那你就得用mocks了。
如果您的 MyMath class 如此简单,我不会使用 Spring 来初始化它。没有必要,更重要的是 - junit 应该很快。因此,您使用 Spring Context 进行的测试不是 运行,而是将其更改为简单的 JUnit 并像普通对象一样创建 MyMath:
public class MyMathTest {
private MyMath myMath = new MyMath();
@Test
public void testSaveAndList() {
assertEquals(5, myMath.add(2, 3));
}
}