Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intellij junit @RunWith not resolved

I have the following test code:

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);
}

}

This is a maven project, the problem is the exact same code would run in eclipse, but not in intellij.

I just can't find a way to resolve @RunWith

screen shot from intellij

like image 759
Z.better Avatar asked Mar 02 '17 09:03

Z.better


People also ask

What happened to @RunWith In JUnit 5?

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation. However, the @RunWith annotation can still be used in JUnit5 for the sake of the backward compatibility. 2. Running Tests With a JUnit4-Based Runner

Why are my JUnit tests not working in IntelliJ?

Simple: your IDE is not configured to for unit testing. In other words: you are missing all the JUnit related classes. You can see that all those JUnit imports are underlined; as IntelliJ simply doesn't know about the JARs that contain the corresponding classes.

Is it possible to run a JUnit test without a runner?

Let's now run the same test in an Eclipse version that supports JUnit5. In this case, we don't need the @RunWith annotation anymore and we can write the test without a runner:

Does IntelliJ use Maven with JUnit?

By the way, note that as of 5.4.0 of JUnit, we can specify the new and very convenient single Maven artifact of junit-jupiter which in turn will supply 8 libraries to your project. @teknopaul (a) By default IntelliJ uses its own bundled copy of Maven.


1 Answers

The @RunWith annotation has been replaced with @ExtendWith in JUnit 5.0 and above (which the latest spring version is now using).

Example:

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

Quoted from Baeldung:

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

Ref: https://www.baeldung.com/junit-5-runwith

like image 192
Basil Musa Avatar answered Oct 04 '22 18:10

Basil Musa