Skip to content

Convert Docs for the Junit5 #2977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 9 additions & 66 deletions docs/test_framework_integration/junit_5.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,9 @@ executed. Containers declared as instance fields will be started and stopped for
unsupported and may have unintended side effects.

*Example:*
```java
@Testcontainers
class MyTestcontainersTests {

// will be shared between test methods
@Container
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();

// will be started before and stopped after each test method
@Container
private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
@Test
void test() {
assertTrue(MY_SQL_CONTAINER.isRunning());
assertTrue(postgresqlContainer.isRunning());
}
}
```

<!--codeinclude-->
[Mixed Lifecycle](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) inside_block:testClass
<!--/codeinclude-->

## Examples

Expand All @@ -55,58 +36,20 @@ To use the Testcontainers extension annotate your test class with `@Testcontaine
To define a restarted container, define an instance field inside your test class and annotate it with
the `@Container` annotation.

```java
@Testcontainers
class SomeTest {

@Container
private MySQLContainer mySQLContainer = new MySQLContainer();

@Test
void someTestMethod() {
String url = mySQLContainer.getJdbcUrl();

// create a connection and run test as normal
}

@Nested
class NestedTests {
<!--codeinclude-->
[Restarted Containers](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java) inside_block:testClass
<!--/codeinclude-->

@Container
private final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();

void nestedTestMethod() {
// top level container is restarted for nested methods
String mySqlUrl = mySQLContainer.getJdbcUrl();

// nested containers are only available inside their nested class
String postgresUrl = postgreSQLContainer.getJdbcUrl();
}
}
}
```

### Shared containers

Shared containers are defined as static fields in a top level test class and have to be annotated with `@Container`.
Note that shared containers can't be declared inside nested test classes.
This is because nested test classes have to be defined non-static and can't therefore have static fields.

```java
@Testcontainers
class SomeTest {

@Container
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();

@Test
void someTestMethod() {
String url = MY_SQL_CONTAINER.getJdbcUrl();

// create a connection and run test as normal
}
}
```
<!--codeinclude-->
[Shared Container](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) lines:18-23,32-33,35-36
<!--/codeinclude-->

## Singleton containers

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.testcontainers.junit.jupiter;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately i had to create a new test class, to have the code representing nearly the same logic as the previous documentation


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import static org.junit.jupiter.api.Assertions.assertTrue;


// testClass {
@Testcontainers
class MixedLifecycleTests {

// will be shared between test methods
@Container
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();

// will be started before and stopped after each test method
@Container
private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");

@Test
void test() {
assertTrue(MY_SQL_CONTAINER.isRunning());
assertTrue(postgresqlContainer.isRunning());
}
}
// }
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

// testClass {
@Testcontainers
class TestcontainersNestedRestartedContainerTests {

@Container
private final GenericContainer topLevelContainer = new GenericContainer("httpd:2.4-alpine")
.withExposedPorts(80);
// }}

private static String topLevelContainerId;

private static String nestedContainerId;

// testClass {
@Test
void top_level_container_should_be_running() {
assertTrue(topLevelContainer.isRunning());
// }}
topLevelContainerId = topLevelContainer.getContainerId();
// testClass {{
}

@Nested
Expand All @@ -34,16 +39,20 @@ class NestedTestCase {

@Test
void both_containers_should_be_running() {
// top level container is restarted for nested methods
assertTrue(topLevelContainer.isRunning());
// nested containers are only available inside their nested class
assertTrue(nestedContainer.isRunning());

// }}}
if (nestedContainerId == null) {
nestedContainerId = nestedContainer.getContainerId();
} else {
assertNotEquals(nestedContainerId, nestedContainer.getContainerId());
}
// testClass {{
}

// }
@Test
void containers_should_not_be_the_same() {
assertNotEquals(topLevelContainer.getContainerId(), nestedContainer.getContainerId());
Expand All @@ -65,5 +74,7 @@ void ids_should_not_change() {
assertNotEquals(nestedContainerId, nestedContainer.getContainerId());
}
}
// testClass {{{
}
}
// }