没有找到与给定的包含项匹配的测试:JUNIT。

huangapple go评论85阅读模式
英文:

No tests found for given includes: JUNIT

问题

我为服务中的方法编写了一个测试,但是测试无法运行。报错信息如下!我严格按照指南的要求进行了操作,没有添加任何新内容。在互联网上关于这个问题有几个解决方案。可能是什么问题呢?

P.S. 我尝试在运行器的 `设置` -> `测试运行器` 中更改了 `Gradle` / `Intelij Idea`,但不起作用。

```java
测试开始于 17:43 ...

> 任务 :compileJava 已更新
> 任务 :processResources 已更新
> 任务 :classes 已更新
> 任务 :compileTestJava
> 任务 :processTestResources 无源
> 任务 :testClasses
> 任务 :test 失败
失败:任务执行失败:'test'。
> 对于给定的包含项找不到测试:[ru.coffeetearea.service.OrderServiceTest.setOrderService](filter.includeTestsMatching)
* 尝试:
使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获取更多日志输出。使用 --scan 获取完整洞察力。
* 获取更多帮助:https://help.gradle.org
1s 内完成的 4 个可操作任务:2 个已执行,2 个最新

build.gradle:

插件 {
    id 'java'
}

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

sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

// Without these options, Mapstruct gives an error on Cyrillic!
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'
    // ...(其他依赖)
}

test {
    useJUnitPlatform()
}

makeOrder() 方法:

public OrderDTO makeOrder(MakeOrderDTO makeOrderDTO) {
    // 方法内容...
}

我的方法测试:

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());
    }
}

没有找到与给定的包含项匹配的测试:JUNIT。


以上是您提供的内容的翻译。

<details>
<summary>英文:</summary>

I wrote a test for my method from the service, but the test won&#39;t run. Gives an error message! I did everything strictly according to the guide, I did not add anything new. There are few solutions to this problem on the Internet. What could be the problem?

P.S. I tried changing in runner `settings` -&gt; `test runner` -&gt; `Gradle` / `Intelij Idea` - not works.

```java
Testing started at 17:43 ...

&gt; Task :compileJava UP-TO-DATE
&gt; Task :processResources UP-TO-DATE
&gt; Task :classes UP-TO-DATE
&gt; Task :compileTestJava
&gt; Task :processTestResources NO-SOURCE
&gt; Task :testClasses
&gt; Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task &#39;:test&#39;.
&gt; 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 &#39;java&#39;
}

group = &#39;com.example&#39;
version = &#39;0.0.1-SNAPSHOT&#39;

sourceCompatibility = &#39;1.8&#39;

repositories {
    mavenCentral()
}

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

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

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

    testImplementation(&#39;org.junit.jupiter:junit-jupiter:5.4.0&#39;)

    testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
        exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
    }
}

test {
    useJUnitPlatform()
}

method makeOrder():

public OrderDTO makeOrder(MakeOrderDTO makeOrderDTO) {

        Long userId = JwtUser.getCurrentUserID();

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

        if (order == null) {
            throw new MainNullPointerException(&quot;Ошибка! Ваша корзина пуста!&quot;);
        }
        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);
    }

My tests for method:

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());
    }
}

没有找到与给定的包含项匹配的测试:JUNIT。

答案1

得分: 1

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

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

没有找到与给定的包含项匹配的测试:JUNIT。

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

没有找到与给定的包含项匹配的测试:JUNIT。

英文:

I believe it may be related to your folder hierarchy.

Try to make your test folder hierarchy exactly the same as you src folder. Example:

没有找到与给定的包含项匹配的测试:JUNIT。

Make sure you have set up your project structure the right sources. See the picture of intellij setup for project structure (right click on project > open module settings > modules > sources)

没有找到与给定的包含项匹配的测试:JUNIT。

huangapple
  • 本文由 发表于 2020年9月12日 22:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63861560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定