Commit 7a0934c7 authored by Philip Riecks's avatar Philip Riecks

update all integration test to run in parallel

parent e3e7769f
......@@ -70,7 +70,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>classes</parallel>
<threadCount>10</threadCount>
<threadCount>2</threadCount>
</configuration>
</plugin>
</plugins>
......
......@@ -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"));
}
......
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<ConfigurableApplicationContext> {
@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
......
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<ConfigurableApplicationContext> {
@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
......
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<ConfigurableApplicationContext> {
@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
......
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<ConfigurableApplicationContext> {
@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<Person> 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<Person> 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<Person> 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());
}
}
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<ConfigurableApplicationContext> {
@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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment