没有找到给定的测试包括:JUNIT

Posted

技术标签:

【中文标题】没有找到给定的测试包括:JUNIT【英文标题】:No tests found for given includes: JUNIT 【发布时间】:2020-12-30 19:59:24 【问题描述】:

我从服务中为我的方法编写了一个测试,但测试不会运行。给出错误信息!我严格按照指南做所有事情,我没有添加任何新内容。 Internet 上很少有解决此问题的方法。可能是什么问题?

附:我尝试更改跑步者 settings -> test runner -> Gradle / Intelij Idea - 不起作用。

Testing started at 17:43 ...

> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [ru.coffeetearea.service.OrderServiceTest.setOrderService](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
4 actionable tasks: 2 executed, 2 up-to-date

build.gradle:

plugins 
    id 'java'


group = 'com.example'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '1.8'

repositories 
    mavenCentral()


// Без этих опций Mapstruct выдает ошибку на кириллицу!
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
//

dependencies 
    // Thymeleaf
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.3.3.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.3.RELEASE'
    // Swagger UI
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
    // Swagger 2
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
    compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.flywaydb/flyway-core
    compile group: 'org.flywaydb', name: 'flyway-core', version: '6.5.1'
    // MapStruct
    implementation 'org.mapstruct:mapstruct:1.3.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'
    // https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
    annotationProcessor('org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5')
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.2.RELEASE'
    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'

    // https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
    testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.5.10'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.3.RELEASE'

    testImplementation('org.junit.jupiter:junit-jupiter:5.4.0')

    testImplementation('org.springframework.boot:spring-boot-starter-test') 
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    


test 
    useJUnitPlatform()

方法 makeOrder():

public OrderDTO makeOrder(MakeOrderDTO makeOrderDTO) 

        Long userId = JwtUser.getCurrentUserID();

        Order order = orderRepository.findByUserIdAndOrderStatus(userId, OrderStatus.NEW);

        if (order == null) 
            throw new MainNullPointerException("Ошибка! Ваша корзина пуста!");
        
        order.setTotalCost(calculateOrderPrice(order));
        order.setAddress(makeOrderDTO.getAddress());
        order.setPhoneNumber(makeOrderDTO.getPhoneNumber());
        order.setDateOrder(new Date());
        order.setOrderStatus(OrderStatus.ACTIVE);

        orderRepository.save(order);

        return orderMapper.orderToOrderDTO(order);
    

我对方法的测试:

package ru.coffeetearea.service;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import ru.coffeetearea.dto.MakeOrderDTO;
import ru.coffeetearea.dto.OrderDTO;
import ru.coffeetearea.mappers.OrderMapper;
import ru.coffeetearea.model.Order;
import ru.coffeetearea.repository.OrderRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
class OrderServiceTest 

    @MockBean
    private OrderRepository orderRepository;

    @MockBean
    private OrderMapper orderMapper;

    private OrderService orderService;


    @Autowired
    public void setOrderService(OrderService orderService) 
        this.orderService = orderService;
    


    @Test
    void makeOrder() 

        MakeOrderDTO makeOrderDTO = new MakeOrderDTO();

        OrderDTO orderDTO = orderService.makeOrder(makeOrderDTO);

        Assert.assertNotNull(orderDTO.getAddress());
        Assert.assertNotNull(orderDTO.getPhoneNumber());
    

【问题讨论】:

【参考方案1】:

我相信这可能与您的文件夹层次结构有关。

尝试使您的测试文件夹层次结构与您的 src 文件夹完全相同。示例:

确保您已设置正确的项目结构来源。查看项目结构的intellij设置图片(右键单击项目>打开模块设置>模块>源)

【讨论】:

我添加了有问题的结构 请同时打开主文件夹 尝试使用在test/java 文件夹上没有正文的方法再创建一个@SpringBootApplication。这是我最后的猜测

以上是关于没有找到给定的测试包括:JUNIT的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 没有找到我使用 Kotlin 和 JUnit 5 进行的测试

Intelij 2019.1更新中断JUnit测试

JUnit5 控制台启动器未找到测试

使用JUnit4编写Solr插件的单元测试,包括创建集合

SpringBoot:以最少的配置运行 Junit 单元测试

在 Android Studio 中运行参数化单元测试时,未找到给定的测试包括错误