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

标签 java spring-boot gradle junit mockito

我从服务中为我的方法编写了一个测试,但测试不会运行。给出错误信息!我严格按照指南做所有事情,我没有添加任何新内容。 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
构建.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());
    }
}
enter image description here

最佳答案

我相信这可能与您的文件夹层次结构有关。
尝试使您的测试文件夹层次结构与您的 src 文件夹完全相同。例子:
enter image description here
确保您已为项目结构设置了正确的来源。查看项目结构的intellij设置图片(右键单击项目>打开模块设置>模块>源)
enter image description here

关于java - 没有找到给定的测试包括 : JUNIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63861560/

相关文章:

java - 为什么我收到 NoClassDefFoundError/ClassNotFoundException

java - SHIFT + ENTER 和 Java 中同一组件中的 ENTER

java - 无法读取 json post 方法 (jackson)

spring-boot - 如何在 Spring-boot 2 中使用 Actuator 修改 prometheus 公开的指标名称

android - 在添加Fabric的崩溃报告后,我无法在gradle中添加新的库

android - Gradle 控制台显示构建成功,但出现错误,并且应用程序未运行

java - 为什么我在 Java 的 main() 中再次声明静态字段可以成功?

java - Eclipse 中的控制台打印顺序

java - jasper 报告多语言-在 spring boot 中生成 pdf 时无法显示字体

java - 更新的信息没有反射(reflect)在mysql数据库中