当前位置: 首页 > 知识库问答 >
问题:

Junit STS-在测试运行程序的Junit 5中找不到测试

司空温书
2023-03-14

我已经从https://howtodoinjava.com/spring-batch/java-config-multiple-steps/'复制了Spring批处理程序。

我从https://docs.spring.io/spring-batch/docs/current/reference/html/testing.html创建了以下JUnit测试用例:

package com.example.demo;                                                                         

import org.springframework.batch.core.JobExecution;                                               
import org.springframework.batch.test.JobLauncherTestUtils;                                       
import org.springframework.batch.test.context.SpringBatchTest;                                    
import org.springframework.beans.factory.annotation.Autowired;                                    
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;                            
import org.springframework.test.context.ContextConfiguration;                                     
import org.springframework.test.context.junit4.SpringRunner;                                      
import org.junit.Assert;                                                                          
import org.junit.jupiter.api.*;                                                                   
import org.junit.runner.RunWith;                                                                  

@SpringBatchTest                                                                                  
@RunWith(SpringRunner.class)                                                                      
@ContextConfiguration(classes=DemoSpringBatch1Application.class)                                  
class DemoSpringBatch1ApplicationTests {                                                          

    @Autowired                                                                                    
    private JobLauncherTestUtils jobLauncherTestUtils;                                            

    @Test                                                                                         
    public void testJob() throws Exception {                                                      
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();                             
        Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());             
    }                                                                                             

}                                                                                                 

我的Maven pom文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo_spring_batch_1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_spring_batch_1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <junit-jupiter.version>5.2.0</junit-jupiter.version>
        <junit-platform.version>1.2.0</junit-platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <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>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--    <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency> -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
    <repository>
    <id>repository.spring.release</id>
    <name>Spring GA Repository</name>
    <url>http://repo.spring.io/release</url>
    </repository>
    </repositories>

</project>
java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/TestPlan;[Lorg/junit/platform/launcher/TestExecutionListener;)V
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

谢谢

共有1个答案

商和颂
2023-03-14

使用@extendwith(SpringRunner.class)代替JUnit4的@extendwith(SpringExtension.class),这是Jupiter的方法。

出于一致性的原因,您还应该使用来自joorg.junit.jupiter.api的断言方法。那么您就可以完全摆脱对JUnit4的依赖。我通常在pom.xml中使用以下子句来排除JUnit4

<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>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>

您还需要surefire插件的最新版本才能使用JUnit5:

<build>
    <pluginManagement>
        <plugins>
            <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
 类似资料:
  • 我使用的是Eclipse Oxygen.3版本(4.7.3)。下面是我的JUnit测试类: 当我运行这个Junit测试时,eclipse不断弹出对话框,告诉我“没有tests found with test runner'JUnit5'”。为什么?

  • 我得堆栈: 想法2019.1.3 Springboot 2.1.6 Java 11 Maven 3.8.0 Groovy 2.5 史巴克1.3 JUnit jupiter 5.5.1 JUnit vintage 5.5.1 GMavenPlus插件2.7.1 我们想开始在Spock测试框架中编写测试。我跟着这个howto,但没有成功。当我尝试运行所有测试时,我的spock测试没有运行。 我能运行一

  • 有一种方法可以运行junit测试,只需在“mvn clean install”命令中添加参数?没有使用@Profile注释测试。

  • 我错过了什么? 编辑: 看来Gradle也不接我的测试。当我转到时,它指示0 test。 在IntelliJ中,我打开Gradle窗口,点击“刷新所有Gradle项目”按钮,刷新库。 然后在我的测试类中,我在类声明的顶部添加了。 当我执行时,结果可以在下面找到:

  • 我的项目是一个多模块分级项目,使用Scala。 测试类位于src/Test/scala/xxx/xxxx/xxx/xxxx/xxxxx/xxxxx下,每次尝试从IDE运行时,都会得到相同的错误: 测试类不是什么花哨的,简单的jUnit测试:

  • 当我在Kotlin中的嵌套类中指定测试时,如下所示。。。 ...当使用gradle运行测试时,JUnit无法识别它。只有被找到并运行。 我使用的是JUnit 5.1、Kotlin 1.2.30和Gradle 4.6。