英文:
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());
}
}
以上是您提供的内容的翻译。
<details>
<summary>英文:</summary>
I wrote a test for my method from the service, but the test won'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` -> `test runner` -> `Gradle` / `Intelij Idea` - not works.
```java
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()
}
method 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);
}
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());
}
}
答案1
得分: 1
我相信这可能与您的文件夹层次结构有关。
尝试使您的测试文件夹层次结构与 src 文件夹完全相同。示例:
确保您已经设置好了正确的项目结构。查看 IntelliJ 设置项目结构的图片(右键单击项目 > 打开模块设置 > 模块 > 来源):
英文:
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:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论