Junit STS - No test found with test runner 'Junit 5

半腔热情 提交于 2021-02-11 13:38:54

问题


I have copied the Spring Batch program from https://howtodoinjava.com/spring-batch/java-config-multiple-steps/'.

I have created the below JUnit test cases from 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());             
    }                                                                                             

}                                                                                                 

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>

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.

I still get the below error:

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 Build path looks like below:

Thanks


回答1:


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

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>

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>


来源:https://stackoverflow.com/questions/59104717/junit-sts-no-test-found-with-test-runner-junit-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!