簡體   English   中英

在 spring 引導應用程序中分離集成測試

[英]Separating integration tests in spring boot app

我正在為 SpringBoot 應用程序編寫測試。 我想將集成測試與單元測試分開。 現在我的項目結構看起來像這樣:

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

有沒有辦法添加這樣的模塊:

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

我希望新的 integrationTest 模塊與測試模塊相同,但我不知道如何配置它。 我設法將其添加為模塊,將 integrationTest/java 目錄標記為測試源根目錄並在其中運行測試,但 @SpringBootTest 無法解析 ApplicationContext,並且我所有的 bean 始終是 null。

當我在測試模塊中編寫相同的測試時,它工作正常。

有沒有辦法做到這一點? 這是分離集成測試的正確方法嗎? 如果沒有,最佳做法是什么?

我在 intelliJ 工作,我使用 gradle 作為 package 經理。

您可以將它們與測試的 rest 一起使用; 應用約定和標記匹配以將它們過濾掉,例如將IntegrationTest附加到 class 名稱並為 JUnit @Tag使用相同的值......然后只需定義/聲明一些 Z7B5CC4FB56E176DC7520F716 任務即可執行它們:

test {
  useJUnitPlatform()
}

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

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

完整示例:

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