Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the three test containers coming from?

I put together a demo JUnit5 project to try out the framework. The project consists of Gradle (4.4), Java (8) and Kotlin (1.2.0) with 4 test cases. I have the following Gradle build script (I've removed most of the boilerplate to keep only the important stuff):

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
    mavenCentral()
}

buildscript {
    ext.kotlin_version = '1.2.0'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

configurations {
    all {
        exclude group: 'junit', module: 'junit'
    }
}

project.ext {
    junitPlatformVersion = '1.0.2'
    junitJupiterVersion = '5.0.2'
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
    testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
}

junitPlatform {
    platformVersion '1.0.2'
    filters {
        engines {
            include 'junit-jupiter'
        }
    }
}

I also have a KotlinTest.kt and a JavaTest.java that have the equivalent test cases:

@Test
fun junit5TestPasses() {
    assertTrue(true)
}

@Test
fun junit5TestFails() {
    assertTrue(false)
}

When I run my tests with gradlew junitPlatformTest, I correctly see 2 tests passing and 2 tests failing. However, I also see "3 containers found". My question is why are there 3 containers found? What are they? I can't seem to find a direct answer about test containers in the JUnit5 user guide that pertains to this scenario.

like image 913
varcharmander Avatar asked Dec 11 '17 18:12

varcharmander


People also ask

What is test containers?

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Why use Testcontainers?

We can build and simulate a whole environment that our application needs with TestContainers, some like databases, queues tools, keycloack or any other authentication tool and an infinity of containers that we can use to make all tests to assure our endpoints/resources/behavior/responses from our microservices.


Video Answer


1 Answers

3 containers = JUnit Jupiter Engine + KotlinTest.class + JavaTest.class

An engine, an implementation of TestEngine, is also considered being a container. Next level is the class containing @Test-annotated methods. Look at the example copied from the user-guide:

├─ JUnit Vintage
│  └─ example.JUnit4Tests
│     └─ standardJUnit4Test ✔
└─ JUnit Jupiter
   ├─ StandardTests
   │  ├─ succeedingTest() ✔
   │  └─ skippedTest() ↷ for demonstration purposes
   └─ A special test case
      ├─ Custom test name containing spaces ✔
      ├─ ╯°□°)╯ ✔
      └─ 😱 ✔

Test run finished after 64 ms
[         5 containers found      ]
[         0 containers skipped    ]
[         5 containers started    ]
[         0 containers aborted    ]
[         5 containers successful ]
[         0 containers failed     ]
[         6 tests found           ]
[         1 tests skipped         ]
[         5 tests started         ]
[         0 tests aborted         ]
[         5 tests successful      ]
[         0 tests failed          ]

Here you see five containers, namely:

  1. JUnit Vintage engine
  2. example.JUnit4Tests class
  3. JUnit Jupiter engine
  4. StandardTests class
  5. A special test case class

All six leaves are tests.

To see a similar tree rendered for your test plan run, add details 'tree' to the Gradle junitPlatform task.

like image 129
Sormuras Avatar answered Nov 02 '22 10:11

Sormuras