Junit STS-未找到测试跑步者'Junit 5的测试 [英] Junit STS - No test found with test runner 'Junit 5

查看:73
本文介绍了Junit STS-未找到测试跑步者'Junit 5的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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文件是:

My Maven pom file is:

<?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提到的依赖项,它们为

I tried changing dependencies mentioned by Gerold and hbattac as Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory. I tried modifying the junit libraries.

我仍然收到以下错误:

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构建路径如下所示:

Java Build path looks like below:

谢谢

推荐答案

代替@RunWith(SpringRunner.class),它是JUnit 4,使用@ExtendWith(SpringExtension.class),这是木星的方法.

Instead of @RunWith(SpringRunner.class), which is JUnit 4, use @ExtendWith(SpringExtension.class) which is Jupiter´s way to do it.

出于一致性原因,您还应该使用org.junit.jupiter.api.Assertions中的断言方法.然后,您可以完全摆脱对JUnit 4的依赖.我通常在pom.xml中使用以下子句来排除JUnit4

You should also use assertion methods from ˋorg.junit.jupiter.api.Assertionsˋ for consistency reasons. Then you can get rid of your dependency on JUnit 4 altogether. I usually have the following clause in pom.xml to exclude 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配合使用:

You also need a recent version of the surefire plugin to work with 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>

这篇关于Junit STS-未找到测试跑步者'Junit 5的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆