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

huangapple go评论66阅读模式
英文:

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

问题

我的Spring Boot应用程序有几个测试类。

右键单击一个测试类,并选择“运行XYZtest”选项,它可以正常工作。

但是,当选择包含该类的文件夹并选择“在com.example.my中运行测试”时,会出现找不到junit.jar的错误:

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

当使用mvn运行所有测试时,它们都可以正常运行。

根据一些建议,我专门在“库”部分中添加了junit.jar,但没有帮助:

"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:

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

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:

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

答案1

得分: 2

我遇到了这个问题,以下是我是如何解决的:

背景:

  • SpringBoot应用程序
  • 使用Maven管理多个模块
  • 在根POM的dependencyManagement中添加junit的Maven依赖项(而不是dependencies,它们的区别可以在这里找到)
  • 打算测试根模块的子模块内的类或文件夹

注:如果您的情况与上述背景不符,此解决方案可能无法解决您的问题。

步骤:

  1. 在您要测试的类或文件夹上右键单击:
    "No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests
  2. 选择更多运行/调试 -> 修改运行配置
  3. 将模块选项更改为您要测试的模块,从根模块中选择
    "No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests
英文:

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:
    "No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests
  2. Choose More Run/Debug -> Modify Run Configuration
  3. Change the module option to the one you want to test from root module
    "No junit.jar" error in Intellij when right clicking a folder, but not when running individual classes of tests

答案2

得分: 1

Spring Boot正在使用JUnit 5。如果你使用Spring Boot的初始装配器网站,你应该有一个依赖项像这样。

<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>

你在你的测试中使用JUnit 4还是5?你可以尝试更新IntelliJ或JUnit插件,这可能会解决你的问题。

英文:

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

&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
		&lt;exclusions&gt;
			&lt;exclusion&gt;
				&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
				&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
			&lt;/exclusion&gt;
		&lt;/exclusions&gt;
	&lt;/dependency&gt;

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

答案3

得分: 0

我成功解决了这个问题,方法是在根目录下的pom.xml文件中添加了Junit Maven依赖。

英文:

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

huangapple
  • 本文由 发表于 2020年8月6日 19:14:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63282382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定