11

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

Steve C
  • 18,876
  • 5
  • 34
  • 37
Z.better
  • 348
  • 1
  • 5
  • 15

2 Answers2

34

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

Basil Musa
  • 8,198
  • 6
  • 64
  • 63
8

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.

See here on how to fix that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248