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

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

No tests found for given includes: JUNIT

问题

  1. 我为服务中的方法编写了一个测试,但是测试无法运行。报错信息如下!我严格按照指南的要求进行了操作,没有添加任何新内容。在互联网上关于这个问题有几个解决方案。可能是什么问题呢?
  2. P.S. 我尝试在运行器的 `设置` -> `测试运行器` 中更改了 `Gradle` / `Intelij Idea`,但不起作用。
  3. ```java
  4. 测试开始于 17:43 ...
  5. > 任务 :compileJava 已更新
  6. > 任务 :processResources 已更新
  7. > 任务 :classes 已更新
  8. > 任务 :compileTestJava
  9. > 任务 :processTestResources 无源
  10. > 任务 :testClasses
  11. > 任务 :test 失败
  12. 失败:任务执行失败:'test'。
  13. > 对于给定的包含项找不到测试:[ru.coffeetearea.service.OrderServiceTest.setOrderService](filter.includeTestsMatching)
  14. * 尝试:
  15. 使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获取更多日志输出。使用 --scan 获取完整洞察力。
  16. * 获取更多帮助:https://help.gradle.org
  17. 1s 内完成的 4 个可操作任务:2 个已执行,2 个最新

build.gradle:

  1. 插件 {
  2. id 'java'
  3. }
  4. group = 'com.example'
  5. version = '0.0.1-SNAPSHOT'
  6. sourceCompatibility = '1.8'
  7. repositories {
  8. mavenCentral()
  9. }
  10. // Without these options, Mapstruct gives an error on Cyrillic!
  11. compileJava.options.encoding = 'UTF-8'
  12. compileTestJava.options.encoding = 'UTF-8'
  13. //
  14. dependencies {
  15. // Thymeleaf
  16. implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
  17. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
  18. compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.3.3.RELEASE'
  19. // ...(其他依赖)
  20. }
  21. test {
  22. useJUnitPlatform()
  23. }

makeOrder() 方法:

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

我的方法测试:

  1. package ru.coffeetearea.service;
  2. import org.junit.Assert;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.boot.test.mock.mockito.MockBean;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import ru.coffeetearea.dto.MakeOrderDTO;
  10. import ru.coffeetearea.dto.OrderDTO;
  11. import ru.coffeetearea.mappers.OrderMapper;
  12. import ru.coffeetearea.model.Order;
  13. import ru.coffeetearea.repository.OrderRepository;
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest
  16. class OrderServiceTest {
  17. @MockBean
  18. private OrderRepository orderRepository;
  19. @MockBean
  20. private OrderMapper orderMapper;
  21. private OrderService orderService;
  22. @Autowired
  23. public void setOrderService(OrderService orderService) {
  24. this.orderService = orderService;
  25. }
  26. @Test
  27. void makeOrder() {
  28. MakeOrderDTO makeOrderDTO = new MakeOrderDTO();
  29. OrderDTO orderDTO = orderService.makeOrder(makeOrderDTO);
  30. Assert.assertNotNull(orderDTO.getAddress());
  31. Assert.assertNotNull(orderDTO.getPhoneNumber());
  32. }
  33. }

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

  1. 以上是您提供的内容的翻译。
  2. <details>
  3. <summary>英文:</summary>
  4. 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?
  5. P.S. I tried changing in runner `settings` -&gt; `test runner` -&gt; `Gradle` / `Intelij Idea` - not works.
  6. ```java
  7. Testing started at 17:43 ...
  8. &gt; Task :compileJava UP-TO-DATE
  9. &gt; Task :processResources UP-TO-DATE
  10. &gt; Task :classes UP-TO-DATE
  11. &gt; Task :compileTestJava
  12. &gt; Task :processTestResources NO-SOURCE
  13. &gt; Task :testClasses
  14. &gt; Task :test FAILED
  15. FAILURE: Build failed with an exception.
  16. * What went wrong:
  17. Execution failed for task &#39;:test&#39;.
  18. &gt; No tests found for given includes: [ru.coffeetearea.service.OrderServiceTest.setOrderService](filter.includeTestsMatching)
  19. * Try:
  20. 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.
  21. * Get more help at https://help.gradle.org
  22. BUILD FAILED in 1s
  23. 4 actionable tasks: 2 executed, 2 up-to-date

build.gradle:

  1. plugins {
  2. id &#39;java&#39;
  3. }
  4. group = &#39;com.example&#39;
  5. version = &#39;0.0.1-SNAPSHOT&#39;
  6. sourceCompatibility = &#39;1.8&#39;
  7. repositories {
  8. mavenCentral()
  9. }
  10. // Без этих опций Mapstruct выдает ошибку на кириллицу!
  11. compileJava.options.encoding = &#39;UTF-8&#39;
  12. compileTestJava.options.encoding = &#39;UTF-8&#39;
  13. //
  14. dependencies {
  15. // Thymeleaf
  16. implementation &#39;org.springframework.boot:spring-boot-starter-thymeleaf&#39;
  17. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
  18. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-thymeleaf&#39;, version: &#39;2.3.3.RELEASE&#39;
  19. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation
  20. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-validation&#39;, version: &#39;2.3.3.RELEASE&#39;
  21. // Swagger UI
  22. compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger-ui&#39;, version: &#39;2.9.2&#39;
  23. // Swagger 2
  24. compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger2&#39;, version: &#39;2.9.2&#39;
  25. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
  26. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot&#39;, version: &#39;2.3.1.RELEASE&#39;
  27. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
  28. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-data-jpa&#39;, version: &#39;2.3.1.RELEASE&#39;
  29. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
  30. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-jdbc&#39;, version: &#39;2.3.1.RELEASE&#39;
  31. // https://mvnrepository.com/artifact/org.postgresql/postgresql
  32. compile group: &#39;org.postgresql&#39;, name: &#39;postgresql&#39;, version: &#39;42.2.14&#39;
  33. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
  34. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-web&#39;, version: &#39;2.3.1.RELEASE&#39;
  35. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
  36. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-data-jpa&#39;, version: &#39;2.3.1.RELEASE&#39;
  37. // https://mvnrepository.com/artifact/org.flywaydb/flyway-core
  38. compile group: &#39;org.flywaydb&#39;, name: &#39;flyway-core&#39;, version: &#39;6.5.1&#39;
  39. // MapStruct
  40. implementation &#39;org.mapstruct:mapstruct:1.3.1.Final&#39;
  41. annotationProcessor &#39;org.mapstruct:mapstruct-processor:1.3.1.Final&#39;
  42. // https://mvnrepository.com/artifact/org.projectlombok/lombok
  43. compileOnly &#39;org.projectlombok:lombok:1.18.12&#39;
  44. annotationProcessor &#39;org.projectlombok:lombok:1.18.12&#39;
  45. // https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
  46. annotationProcessor(&#39;org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5&#39;)
  47. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
  48. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-security&#39;, version: &#39;2.3.2.RELEASE&#39;
  49. // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
  50. compile group: &#39;io.jsonwebtoken&#39;, name: &#39;jjwt&#39;, version: &#39;0.9.1&#39;
  51. // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
  52. compile group: &#39;javax.xml.bind&#39;, name: &#39;jaxb-api&#39;, version: &#39;2.4.0-b180830.0359&#39;
  53. // https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
  54. testCompile group: &#39;org.mockito&#39;, name: &#39;mockito-junit-jupiter&#39;, version: &#39;3.5.10&#39;
  55. // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
  56. testCompile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-test&#39;, version: &#39;2.3.3.RELEASE&#39;
  57. testImplementation(&#39;org.junit.jupiter:junit-jupiter:5.4.0&#39;)
  58. testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
  59. exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
  60. }
  61. }
  62. test {
  63. useJUnitPlatform()
  64. }

method makeOrder():

  1. public OrderDTO makeOrder(MakeOrderDTO makeOrderDTO) {
  2. Long userId = JwtUser.getCurrentUserID();
  3. Order order = orderRepository.findByUserIdAndOrderStatus(userId, OrderStatus.NEW);
  4. if (order == null) {
  5. throw new MainNullPointerException(&quot;Ошибка! Ваша корзина пуста!&quot;);
  6. }
  7. order.setTotalCost(calculateOrderPrice(order));
  8. order.setAddress(makeOrderDTO.getAddress());
  9. order.setPhoneNumber(makeOrderDTO.getPhoneNumber());
  10. order.setDateOrder(new Date());
  11. order.setOrderStatus(OrderStatus.ACTIVE);
  12. orderRepository.save(order);
  13. return orderMapper.orderToOrderDTO(order);
  14. }

My tests for method:

  1. package ru.coffeetearea.service;
  2. import org.junit.Assert;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.boot.test.mock.mockito.MockBean;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import ru.coffeetearea.dto.MakeOrderDTO;
  10. import ru.coffeetearea.dto.OrderDTO;
  11. import ru.coffeetearea.mappers.OrderMapper;
  12. import ru.coffeetearea.model.Order;
  13. import ru.coffeetearea.repository.OrderRepository;
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest
  16. class OrderServiceTest {
  17. @MockBean
  18. private OrderRepository orderRepository;
  19. @MockBean
  20. private OrderMapper orderMapper;
  21. private OrderService orderService;
  22. @Autowired
  23. public void setOrderService(OrderService orderService) {
  24. this.orderService = orderService;
  25. }
  26. @Test
  27. void makeOrder() {
  28. MakeOrderDTO makeOrderDTO = new MakeOrderDTO();
  29. OrderDTO orderDTO = orderService.makeOrder(makeOrderDTO);
  30. Assert.assertNotNull(orderDTO.getAddress());
  31. Assert.assertNotNull(orderDTO.getPhoneNumber());
  32. }
  33. }

没有找到与给定的包含项匹配的测试: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:

确定