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
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
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.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With