简体   繁体   English

java.lang.Exception: 没有可运行的方法 junit springboot2

[英]java.lang.Exception: No runnable methods junit springboot2

I am writing Junits for a springboot application.我正在为 springboot 应用程序编写 Junits。 when i run the junit i am getting that there are no methods to run.当我运行junit时,我发现没有方法可以运行。 below is the code junit:下面是代码junit:

    @RunWith(Suite.class)
    @SuiteClasses({ EmployeeServiceApplication.class })
    public class TestEmployeeService {

        @Mock
        EmployeeRepository empRepo;

        @Autowired
        EmployeeService service;


        @BeforeEach
        void setMockOutput() {
            HashSet<EmployeeView> empSet = new HashSet<>();
            empSet.add(new EmployeeView(1, "firstName", "lastName", "05/30/1986", "EE"));
            when(empRepo.getEmployeeList()).thenReturn(empSet);
        }

        @Test
        public void testGetEmployeeList() {
             Assert.notEmpty(service.getEmployeeList());

    }

pom.xml: pom.xml:

    <?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.employeeService</groupId>
        <artifactId>employeeService</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>employeeService</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.4.0</version>
            </dependency>

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.4.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

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

    </project>

am i missing something ?我错过了什么吗? i tried lot of options from stackoverflow and they didn't work.我尝试了 stackoverflow 中的很多选项,但它们都不起作用。 and below are the things that i have tried with 1. @SpringBootTest 2. @mockRunner 3. @SpringBootTest (classes)以下是我尝试过的东西 1.@SpringBootTest 2.@mockRunner 3.@SpringBootTest(类)

Stack Trace:堆栈跟踪:

java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
    at org.junit.runners.Suite.<init>(Suite.java:102)
    at org.junit.runners.Suite.<init>(Suite.java:70)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:107)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

page imports:页面导入:

package com.employeeService.employeeService.service;

import static org.mockito.Mockito.when;

import java.util.HashSet;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import com.employeeService.employeeService.EmployeeServiceApplication;
import com.employeeService.employeeService.service.repository.EmployeeRepository;

You are mixing JUnit4 (RunWith) with JUnit5 (BeforeEach).您正在混合 JUnit4 (RunWith) 和 JUnit5 (BeforeEach)。 I guess the test annotation is from JUpiter/JUnit5 that's why the runner does not find any test methods.我猜测试注释来自 JUpiter/JUnit5,这就是为什么跑步者找不到任何测试方法。

You have to stick with either JUnit4 or Jupiter.您必须坚持使用 JUnit4 或 Jupiter。 Watch your imports!注意你的进口!

Re-importing with import org.junit.Test;使用 import org.junit.Test 重新导入; and removing the jupiter import fixed the problem并删除木星导入解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会得到 java.lang.Exception: No runnable methods 异常在这个测试类中? - Why do I get a java.lang.Exception: No runnable methods exception in this test class? java.lang.Exception:检测到明显的连接泄漏错误 - java.lang.Exception: Apparent connection leak detected Error 如何在Java(SpringBoot2)和GCP数据存储中创建种子数据 - How to create Seed Data in Java(SpringBoot2) and GCP Datastore springboot2 + webflux + websocket - springboot2 +webflux + websocket Springboot2和OAuth - Springboot2 and oauth java.lang.Exception: 不是实体: class com.domain.package.User - java.lang.Exception: Not an entity: class com.domain.package.User How to avoid catching OutOfMemoryError in java and how to resolve class java.lang.OutOfMemoryError cannot be cast to class java.lang.Exception issue - How to avoid catching OutOfMemoryError in java and how to resolve class java.lang.OutOfMemoryError cannot be cast to class java.lang.Exception issue 如何使用SpringBoot2,JUnit5和Kotlin将配置属性注入到单元测试中 - How can I inject config properties into a unit test, using SpringBoot2, JUnit5, and Kotlin 如何在多模块项目中通过 gradle 而不是 IntelliJ 与 JUnit5 和 SpringBoot2 运行测试 - How to run tests by gradle instead of intelliJ with JUnit5 and SpringBoot2 in multi module project SpringBoot2多个数据源配置 - SpringBoot2 multiple datasource confguration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM