简体   繁体   中英

"No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests

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:

在此处输入图像描述

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:

"

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.

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:
    在此处输入图像描述
  2. Choose More Run/Debug -> Modify Run Configuration
  3. Change the module option to the one you want to test from root module在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM