简体   繁体   English

在 spring 引导应用程序中分离集成测试

[英]Separating integration tests in spring boot app

I am writing tests for SpringBoot application.我正在为 SpringBoot 应用程序编写测试。 I would like to separate integration tests from unit tests.我想将集成测试与单元测试分开。 Now my project structure looks something like this:现在我的项目结构看起来像这样:

├── Demo
│   ├── src
│   │   ├── main
|   |   |   └──java
│   │   ├── test
|   |   |   └──java

Is there a way to add a module like this:有没有办法添加这样的模块:

├── Demo
│   ├── src
│   │   ├── main
|   |   |   └──java
│   │   ├── integrationTest
|   |   |   └──java
│   │   ├── test
|   |   |   └──java

I would like that new integrationTest module to act the same as test module, but I don't know how to configure it.我希望新的 integrationTest 模块与测试模块相同,但我不知道如何配置它。 I managed to add it as module, mark integrationTest/java dir as Test Sources Root and run test in it but @SpringBootTest cannot resolve ApplicationContext, and all of my beans are always null.我设法将其添加为模块,将 integrationTest/java 目录标记为测试源根目录并在其中运行测试,但 @SpringBootTest 无法解析 ApplicationContext,并且我所有的 bean 始终是 null。

When I write the same test in test module, it works fine.当我在测试模块中编写相同的测试时,它工作正常。

Is there a way to do this?有没有办法做到这一点? Is this the right way to separate integration tests?这是分离集成测试的正确方法吗? If not, what is the best practice?如果没有,最佳做法是什么?

I am working in intelliJ and I am using gradle as package manager.我在 intelliJ 工作,我使用 gradle 作为 package 经理。

You can have them with the rest of the tests;您可以将它们与测试的 rest 一起使用; apply a convention and tag-match that to filter them out, something like appending IntegrationTest to the class name(s) and using the same value for the JUnit @Tag ... then just define/declare some Gradle tasks to execute them:应用约定和标记匹配以将它们过滤掉,例如将IntegrationTest附加到 class 名称并为 JUnit @Tag使用相同的值......然后只需定义/声明一些 Z7B5CC4FB56E176DC7520F716 任务即可执行它们:

test {
  useJUnitPlatform()
}

task integrationTests(type: Test) {
  filter { includes = ["IntegrationTest"] }
  useJUnitPlatform()
}

task unitTests(type: Test) {
  filter { includes = ["UnitTest"] }
  useJUnitPlatform()
}

Full example:完整示例:

package tld.domain.support;

public final class Category {
  public static final INTEGRATION_TEST = "IntegrationTest";

  public static final UNIT_TEST = "UnitTest";
}
import tld.domain.support.Category;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag(Category.INTEGRATION_TEST)
final class MyIntegrationTest {
  @Test
  void testFoo() {
    // ...
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM