Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
blog-tutorials
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
測試專案
blog-tutorials
Commits
7a0934c7
Commit
7a0934c7
authored
Jun 10, 2018
by
Philip Riecks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update all integration test to run in parallel
parent
e3e7769f
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
95 additions
and
66 deletions
+95
-66
testcontainers/pom.xml
testcontainers/pom.xml
+1
-1
testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java
...va/de/rieckpil/blog/testcontainers/PersonsController.java
+1
-0
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java
...kpil/blog/testcontainers/CreatePersonIntegrationTest.java
+16
-10
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java
...kpil/blog/testcontainers/DeletePersonIntegrationTest.java
+16
-11
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java
...pil/blog/testcontainers/GetAllPersonsIntegrationTest.java
+16
-11
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java
...pil/blog/testcontainers/GetPersonByIdIntegrationTest.java
+29
-23
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java
...l/blog/testcontainers/TestcontainersApplicationTests.java
+16
-10
No files found.
testcontainers/pom.xml
View file @
7a0934c7
...
...
@@ -70,7 +70,7 @@
<artifactId>
maven-surefire-plugin
</artifactId>
<configuration>
<parallel>
classes
</parallel>
<threadCount>
10
</threadCount>
<threadCount>
2
</threadCount>
</configuration>
</plugin>
</plugins>
...
...
testcontainers/src/main/java/de/rieckpil/blog/testcontainers/PersonsController.java
View file @
7a0934c7
...
...
@@ -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"
));
}
...
...
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/CreatePersonIntegrationTest.java
View file @
7a0934c7
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
...
...
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/DeletePersonIntegrationTest.java
View file @
7a0934c7
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
...
...
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetAllPersonsIntegrationTest.java
View file @
7a0934c7
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
...
...
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/GetPersonByIdIntegrationTest.java
View file @
7a0934c7
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
());
}
}
testcontainers/src/test/java/de/rieckpil/blog/testcontainers/TestcontainersApplicationTests.java
View file @
7a0934c7
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment