何时使用@ExtendWith(SpringExtension.class)和@SpringBootTest测试类

xggvc2p6  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(68)

我现在想知道这段代码与

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
public class TestClass1 {
    
    // some code

}

字符串
这个..

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass2 {
    
    // some code

}


第一段代码能做什么第二段代码不能做的事?反之亦然:第二段代码能做什么第一段代码不能做的事?
对我来说,在这两种情况下,我们都可以:

  • 使用Spring上下文
  • 使用junit

那么..有什么区别呢?

2ledvvac

2ledvvac1#

你所需要知道的一切
第一个月
1.用于运行集成测试并加载Spring TestContext
1.也可以使用JUnit注解
1.在配置方面没有丰富的API
@SpringBootTest
1.用于运行集成测试并加载Spring TestContext
1.也可以使用JUnit注解
1.提供丰富的API来配置测试配置
1.推荐使用,因为它支持新功能且易于使用

相关问题