2

My Spring boot application has several tests classes.

When right-clicking a test class, and selecting the option to Run XYZtest it works with no problem.

But when selecting the folder that contains that class and selecting Run test in com.example.my I get an error of No junit.jar:

enter image description here

When running all the tests from mvn they run with no problems.

Following some suggestions, I added the junit.jar specifically in the Library section, but it didn't help:

"

riorio
  • 6,500
  • 7
  • 47
  • 100
  • 1
    Try Invalidate Cache and Restart File > Invalidate Caches/Restart... Also, Try reloading the maven project from. – rimonmostafiz Aug 07 '20 at 15:58
  • @rimonmostafiz yes. I tried it, but it didn't help. – riorio Aug 09 '20 at 05:00
  • Make sure you have junit library of the right version added to every module's dependencies where you have junit tests. In case of Maven-based project - check that the junit dependency is present in every module's pom.xml file. – Andrey Aug 11 '20 at 13:19

3 Answers3

2

I met this problem, this is how I soloved it:

Context:

  • SpringBoot application
  • Use maven to manage multiple modules
  • Add junit's maven dependency in root POM's dependencyManagement(rather than dependencies, their differences can be found here)
  • Intend to test class or folder inside one of the root module's child module

PS: If your situation does not match the context above, this solution may not solve your problem.

Steps

  1. right click at the class or folder you want to test:
    enter image description here
  2. Choose More Run/Debug -> Modify Run Configuration
  3. Change the module option to the one you want to test from root module enter image description here
SteveHu
  • 82
  • 9
1

Spring boot is using JUnit 5. If you use the spring boot initializr website, you should have a dependency like this.

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Did you use JUnit 4 or 5 in your tests ? You can try to update IntelliJ or JUnit plugin, this may solve your problem.

Cyril G.
  • 1,879
  • 2
  • 5
  • 19
0

I managed to solve the issue by adding the Junit Maven dependancy in the root pom.xml file.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31858417) – Sumit Sharma May 29 '22 at 10:34