【问题标题】:Junit STS - No test found with test runner 'Junit 5Junit STS - 未找到测试运行程序“Junit 5”的测试
【发布时间】:2020-03-25 01:07:45
【问题描述】:

我从https://howtodoinjava.com/spring-batch/java-config-multiple-steps/'复制了Spring Batch程序。

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

我尝试将 Gerold 和 hbattac 提到的依赖项更改为 Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory。 我尝试修改junit库。

我仍然收到以下错误:

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)

Java 构建路径如下所示:

谢谢

【问题讨论】:

    标签: junit5


    【解决方案1】:

    使用 @ExtendWith(SpringExtension.class) 代替 JUnit 4 的 @RunWith(SpringRunner.class),这是 Jupiter 的方式。

    出于一致性原因,您还应该使用 ˋorg.junit.jupiter.api.Assertionsˋ 中的断言方法。然后,您可以完全摆脱对 JUnit 4 的依赖。我通常在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 插件才能使用 JUnit 5:

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

    【讨论】:

    • 导入 org.springframework.batch.core.JobExecution;导入 org.springframework.batch.test.JobLauncherTestUtils;导入 org.springframework.batch.test.context.SpringBatchTest;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.test.context.ContextConfiguration;导入 org.springframework.test.context.junit.jupiter.SpringExtension;导入 org.junit.jupiter.api.extension.ExtendWith;导入静态 org.junit.jupiter.api.Assertions.assertEquals;导入 org.junit.jupiter.api.Test;
    • @SpringBatchTest @ExtendWith(SpringExtension.class) @ContextConfiguration(classes=DemoSpringBatch1Application.class) class DemoSpringBatch1ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test public void testJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode()); } }
    猜你喜欢
    • 2011-01-20
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2018-10-11
    • 2022-10-18
    • 2020-04-07
    相关资源
    最近更新 更多