java - intellij junit @RunWith 未解决

标签 java eclipse intellij-idea junit

我有以下测试代码:

package soundSystem;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(classes = CDPlayerConfig.class)

public class SonyCDPlayerTest {

@Autowired
private ICompactDisk cd;

@Test
public void cdShouldNotBeNull() {
    assertNotNull(cd);
}

}

这是一个 maven 项目,问题是完全相同的代码可以在 eclipse 中运行,但不能在 intellij 中运行。

我只是找不到解决@RunWith 的方法

screen shot from intellij

最佳答案

@RunWith 注解在 JUnit 5.0 及更高版本中被替换为 @ExtendWith(最新的 spring 版本正在使用)。

例子:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

引自 Baeldung:

Note that SpringExtension.class is provided by Spring 5 and integrates the Spring TestContext Framework into JUnit 5.

引用:https://www.baeldung.com/junit-5-runwith

关于java - intellij junit @RunWith 未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42550874/

相关文章:

java - 读取文本文件(我已经知道)然后在 GUI 中显示它?

java - LDAP 访问 - javax.naming.CommunicationException : simple bind failed

eclipse - 使用 OpenShift 远程调试 tomcat

gradle - 如何使用gradle idea任务设置Intellij的默认设置?

java - 在 jdk 11 中使用 "var"时编译时出现问题

java - Infinispan:如何异步调用EntryProcessor?

java - 匹配标签内的内容

java - 如何在 Eclipse 中完全禁用 Oomph?

android - @android :color/holo_blue_light requires API level 14 - previously it was OK

powershell - 当karma:interactive运行时,Intellij无法保存文件