简体   繁体   中英

Separating integration tests in spring boot app

I am writing tests for SpringBoot application. 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. 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.

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.

You can have them with the rest of the tests; 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:

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() {
    // ...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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