首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在junit5中使用SpringRunner有什么特殊的配置吗?

在junit5中使用SpringRunner有什么特殊的配置吗?
EN

Stack Overflow用户
提问于 2020-04-08 04:16:37
回答 4查看 11.5K关注 0票数 9

我的微服务项目基于spring-boot框架,所有的单元测试都使用spring runner。

代码语言:javascript
运行
复制
@RunWith(SpringRunner.class)

添加此注释后,将导入下列库:

代码语言:javascript
运行
复制
import org.springframework.test.context.junit4.SpringRunner;

如何将我的测试类设置为使用junit5运行?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-09-17 23:44:41

从构建路径中删除JUnit4。

例如:

代码语言:javascript
运行
复制
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @Before
    public void setUp() {
        ...
    }

    @Test
    public void testMethod() {
        Assert.assertTrue(...);
    }
}

将成为

代码语言:javascript
运行
复制
@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @BeforeEach
    public void setUp() {
        ...
    }
    @Test
    public void testMethod() {
       Assertions.assertTrue(...);
    }
}
票数 7
EN

Stack Overflow用户

发布于 2020-04-08 13:36:48

使用JUnit Jupiter (又称JUnit 5)不再需要ˋ@RunWith(SpringRunner.class)ˋ,因为这是JUnit 4机制。最近发布的Spring/Spring Boot 5支持开箱即用,例如通过使用ˋspring-boot-starter- JUnitˋ。

我建议在Maven/Gradle文件中排除对JUnit 4的依赖,以减少混淆JUnit 4和5特性的可能性。

这里有一篇文章展示了基础知识:https://howtodoinjava.com/spring-boot2/testing/junit5-with-spring-boot2/

票数 12
EN

Stack Overflow用户

发布于 2021-04-12 09:43:12

Spring2.4似乎包含了JUnit 5,并使其成为默认的开箱即用版本。

除了将@RunWith(SpringJUnit4ClassRunner.class)更新为@ExtendWith(SpringExtension.class)之外,我还必须在build.gradle中添加以下代码才能真正运行测试:

代码语言:javascript
运行
复制
test {
    useJUnitPlatform {}
}

这最后一步可能是因为JUnit 4是我的一个依赖项的依赖项,但我读到的所有其他东西都表明这不是必需的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61088682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档