diff --git a/testcontainers/pom.xml b/testcontainers/pom.xml index e0168ae5fb36f5cafe5db93c29c783cd005e9575..2856e21b5ae722634d00b2d9c377082c627cf3b4 100644 --- a/testcontainers/pom.xml +++ b/testcontainers/pom.xml @@ -70,7 +70,7 @@ maven-surefire-plugin classes - 10 + 2 diff --git a/testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java b/testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java index 58f53c02cb7050be6ecfdb44346b53ecc1106be1..0edd7cca1794b39651eb0249b3e6382019d41e24 100644 --- a/testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java +++ b/testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java @@ -22,6 +22,7 @@ public class PersonsController { @GetMapping("/{id}") public Person getPersonById(@PathVariable("id") Long id) { + return personRepository.findById(id).orElseThrow(() -> new NoPersonFoundException("Person with id:" + id + " not found")); } diff --git a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java index e982a5bc9e83f36ab2eea97db72bc84c16f0929c..ff8a01938a59289145cc200f008ad306f6d0add6 100644 --- a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java +++ b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java @@ -1,16 +1,18 @@ package de.rieckpil.blog.testcontainers; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.PostgreSQLContainer; import java.util.HashMap; @@ -21,7 +23,7 @@ import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Transactional +@ContextConfiguration(initializers = CreatePersonIntegrationTest.Initializer.class) public class CreatePersonIntegrationTest { @ClassRule @@ -36,13 +38,17 @@ public class CreatePersonIntegrationTest { public TestRestTemplate testRestTemplate = new TestRestTemplate(); - @BeforeClass - public static void beforeClass() { - - System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword()); - System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername()); - + public static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), + "spring.datasource.password=" + postgreSQLContainer.getPassword(), + "spring.datasource.username=" + postgreSQLContainer.getUsername() + ); + values.applyTo(configurableApplicationContext); + } } @Test diff --git a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java index 03d06b24bc1c31b90c3aa4a7754e2d0b7e6afa69..742fe4465db29aa485384af857621114f8c18daa 100644 --- a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java +++ b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java @@ -1,16 +1,18 @@ package de.rieckpil.blog.testcontainers; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.PostgreSQLContainer; import static org.junit.Assert.assertEquals; @@ -18,7 +20,7 @@ import static org.junit.Assert.assertFalse; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Transactional +@ContextConfiguration(initializers = DeletePersonIntegrationTest.Initializer.class) public class DeletePersonIntegrationTest { @ClassRule @@ -33,14 +35,17 @@ public class DeletePersonIntegrationTest { public TestRestTemplate testRestTemplate = new TestRestTemplate(); - @BeforeClass - public static void beforeClass() { - - System.out.println(postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword()); - System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername()); - + public static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), + "spring.datasource.password=" + postgreSQLContainer.getPassword(), + "spring.datasource.username=" + postgreSQLContainer.getUsername() + ); + values.applyTo(configurableApplicationContext); + } } @Test diff --git a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java index 22f6956dfb309aa1422612e143a64984bc551bcf..d185408a5f4fa737456fabac9066bb1bf27faadd 100644 --- a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java +++ b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java @@ -1,17 +1,19 @@ package de.rieckpil.blog.testcontainers; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.PostgreSQLContainer; import java.util.Arrays; @@ -23,7 +25,7 @@ import static org.junit.Assert.assertTrue; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Transactional +@ContextConfiguration(initializers = GetAllPersonsIntegrationTest.Initializer.class) public class GetAllPersonsIntegrationTest { @ClassRule @@ -38,14 +40,17 @@ public class GetAllPersonsIntegrationTest { public TestRestTemplate testRestTemplate = new TestRestTemplate(); - @BeforeClass - public static void beforeClass() { - - System.out.println(postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword()); - System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername()); - + public static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), + "spring.datasource.password=" + postgreSQLContainer.getPassword(), + "spring.datasource.username=" + postgreSQLContainer.getUsername() + ); + values.applyTo(configurableApplicationContext); + } } @Test diff --git a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java index 84c980406f4363d46f6b66ad13a0a9e9c25bcd40..45357e267bcf433b43219133624eda4d6e032666 100644 --- a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java +++ b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java @@ -1,19 +1,20 @@ package de.rieckpil.blog.testcontainers; -import org.junit.BeforeClass; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.PostgreSQLContainer; import static org.junit.Assert.assertEquals; @@ -21,7 +22,7 @@ import static org.junit.Assert.assertNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Transactional +@ContextConfiguration(initializers = GetPersonByIdIntegrationTest.Initializer.class) public class GetPersonByIdIntegrationTest { @ClassRule @@ -36,13 +37,28 @@ public class GetPersonByIdIntegrationTest { public TestRestTemplate testRestTemplate = new TestRestTemplate(); - @BeforeClass - public static void beforeClass() { + public static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), + "spring.datasource.password=" + postgreSQLContainer.getPassword(), + "spring.datasource.username=" + postgreSQLContainer.getUsername() + ); + values.applyTo(configurableApplicationContext); + } + } + + @Test + public void testNotExistingPersonByIdShouldReturn404() { + + ResponseEntity result = testRestTemplate.getForEntity("http://localhost:" + localPort + + "/api/persons/42", Person.class); - System.out.println(postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword()); - System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername()); + assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode()); + assertNull(result.getBody().getName()); + assertNull(result.getBody().getId()); } @@ -50,6 +66,9 @@ public class GetPersonByIdIntegrationTest { @Sql("/testdata/FILL_FOUR_PERSONS.sql") public void testExistingPersonById() { + System.out.println(personRepository.findAll().size()); + + ResponseEntity result = testRestTemplate.getForEntity("http://localhost:" + localPort + "/api/persons/1", Person.class); @@ -61,17 +80,4 @@ public class GetPersonByIdIntegrationTest { } - @Test - public void testNotExistingPersonByIdShouldReturn404() { - - personRepository.deleteAll(); - - ResponseEntity result = testRestTemplate.getForEntity("http://localhost:" + localPort + - "/api/persons/42", Person.class); - - assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode()); - assertNull(result.getBody().getName()); - assertNull(result.getBody().getId()); - - } } diff --git a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java index aea218d5cb5e74d58fb7e6bfafa01769434c09a8..aae2634aa42c2aae8af5b925d57ae5178be406cf 100644 --- a/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java +++ b/testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java @@ -1,30 +1,36 @@ package de.rieckpil.blog.testcontainers; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.PostgreSQLContainer; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Transactional +@ContextConfiguration(initializers = TestcontainersApplicationTests.Initializer.class) public class TestcontainersApplicationTests { @ClassRule public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer().withPassword("inmemory") .withUsername("inmemory"); - @BeforeClass - public static void beforeClass() { - - System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl()); - System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword()); - System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername()); - + public static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), + "spring.datasource.password=" + postgreSQLContainer.getPassword(), + "spring.datasource.username=" + postgreSQLContainer.getUsername() + ); + values.applyTo(configurableApplicationContext); + } } @Test