Skip to content

Commit 2857764

Browse files
committed
spring-testing-4
1 parent 36e107d commit 2857764

19 files changed

+516
-0
lines changed

testing-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<module>spring-mockito</module>
6060
<module>spring-testing-2</module>
6161
<module>spring-testing-3</module>
62+
<module>spring-testing-4</module>
6263
<module>spring-testing</module>
6364
<module>testing-assertions</module>
6465
<module>test-containers</module>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-testing-4</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<name>spring-testing-4</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-boot-3</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../../parent-boot-3</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.zaxxer</groupId>
24+
<artifactId>HikariCP</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.postgresql</groupId>
28+
<artifactId>postgresql</artifactId>
29+
</dependency>
30+
31+
<!-- test -->
32+
<dependency>
33+
<groupId>com.github.seregamorph</groupId>
34+
<artifactId>spring-test-smart-context</artifactId>
35+
<version>0.14</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-jdbc</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.testcontainers</groupId>
50+
<artifactId>postgresql</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<properties>
56+
<java.version>17</java.version>
57+
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
58+
</properties>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-surefire-plugin</artifactId>
65+
<version>${maven-surefire-plugin.version}</version>
66+
<configuration>
67+
<!-- Include test classes with names matching the pattern -->
68+
<includes>
69+
<include>**/*Test.java</include>
70+
</includes>
71+
</configuration>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-failsafe-plugin</artifactId>
76+
<version>${maven-surefire-plugin.version}</version>
77+
<executions>
78+
<execution>
79+
<goals>
80+
<goal>integration-test</goal>
81+
<goal>verify</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/articles")
10+
public class ArticlesController {
11+
12+
@GetMapping("/{id}")
13+
public String get(@PathVariable("id") long id) {
14+
return "Content " + id;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import com.github.seregamorph.testsmartcontext.SmartDirtiesContextTestExecutionListener;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.springframework.test.context.ActiveProfiles;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.TestExecutionListeners;
8+
import org.springframework.test.context.junit.jupiter.SpringExtension;
9+
10+
// Avoid putting @DirtiesContext on the integration test super class
11+
// @DirtiesContext
12+
// Use SmartDirtiesContextTestExecutionListener instead
13+
@ContextConfiguration(classes = {
14+
SampleBeanTestConfiguration.class
15+
})
16+
@ActiveProfiles("test")
17+
@TestExecutionListeners(listeners = {
18+
SmartDirtiesContextTestExecutionListener.class,
19+
}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
20+
@ExtendWith(SpringExtension.class)
21+
public abstract class AbstractIntegrationTest {
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.TestPropertySource;
7+
8+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
9+
@EnableAutoConfiguration
10+
@ContextConfiguration(classes = {
11+
PostgresTestConfiguration.class,
12+
DataSourceTestConfiguration.class,
13+
ArticlesController.class
14+
})
15+
@TestPropertySource(properties = {
16+
"parameter = value"
17+
})
18+
public abstract class AbstractWebIntegrationTest extends AbstractIntegrationTest {
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import com.github.seregamorph.testsmartcontext.jdbc.LateInitDataSource;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import org.postgresql.Driver;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.testcontainers.containers.PostgreSQLContainer;
11+
12+
import javax.sql.DataSource;
13+
14+
@Configuration
15+
public class DataSourceTestConfiguration {
16+
17+
private static final Logger logger = LoggerFactory.getLogger(DataSourceTestConfiguration.class);
18+
19+
// Avoid creating TestContainer objects which are not spring-managed beans
20+
// @Bean
21+
// public DataSource dataSource() {
22+
// // not a manageable bean!
23+
// var container = new PostgreSQLContainer("postgres:9.6");
24+
// container.start();
25+
// return createDataSource("main", container);
26+
// }
27+
28+
@Bean
29+
public DataSource dataSource(PostgreSQLContainer<?> postgres) {
30+
return createDataSource("main", postgres);
31+
}
32+
33+
private static DataSource createDataSource(String name, PostgreSQLContainer<?> postgres) {
34+
// todo schema migrations, test data insertion, etc.
35+
if (postgres.isRunning()) {
36+
// already running - create direct dataSource
37+
logger.info("Eagerly initializing pool {}", name);
38+
return createHikariDataSourceForContainer(name, postgres);
39+
} else {
40+
// initialize lazily on first getConnection
41+
logger.info("Pool {} will be initialized lazily", name);
42+
return new LateInitDataSource(name, () -> {
43+
logger.info("Starting container for pool {}", name);
44+
postgres.start();
45+
return createHikariDataSourceForContainer(name, postgres);
46+
});
47+
}
48+
}
49+
50+
private static HikariDataSource createHikariDataSourceForContainer(String name, PostgreSQLContainer<?> container) {
51+
var hikariDataSource = new HikariDataSource();
52+
hikariDataSource.setUsername(container.getUsername());
53+
hikariDataSource.setPassword(container.getPassword());
54+
hikariDataSource.setMinimumIdle(0);
55+
hikariDataSource.setMaximumPoolSize(50);
56+
hikariDataSource.setIdleTimeout(10000);
57+
hikariDataSource.setConnectionTimeout(10000);
58+
hikariDataSource.setAutoCommit(true);
59+
hikariDataSource.setPoolName(name);
60+
hikariDataSource.setDriverClassName(Driver.class.getName());
61+
hikariDataSource.setJdbcUrl(container.getJdbcUrl());
62+
return hikariDataSource;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.testcontainers.containers.PostgreSQLContainer;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
@Configuration
9+
public class PostgresTestConfiguration {
10+
11+
private static PostgreSQLContainer<?> postgres;
12+
13+
// override destroy method to empty to avoid closing docker container
14+
// bean on closing spring context
15+
@Bean(destroyMethod = "")
16+
public PostgreSQLContainer<?> postgresContainer() {
17+
synchronized (PostgresTestConfiguration.class) {
18+
if (postgres == null) {
19+
postgres = new PostgreSQLContainer<>(DockerImageName.parse("postgres:14")
20+
.asCompatibleSubstituteFor("postgres"));
21+
}
22+
return postgres;
23+
}
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.java.baeldung.spring.test;
2+
3+
public class SampleBean {
4+
5+
public String getValue() {
6+
return "default";
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import org.springframework.boot.test.context.TestConfiguration;
4+
import org.springframework.context.annotation.Import;
5+
6+
@Import({
7+
SampleBean.class,
8+
SampleService.class
9+
})
10+
@TestConfiguration
11+
public class SampleBeanTestConfiguration {
12+
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.java.baeldung.spring.test;
2+
3+
import jakarta.annotation.PreDestroy;
4+
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ScheduledExecutorService;
7+
import java.util.concurrent.TimeUnit;
8+
9+
public class SampleService {
10+
11+
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(16);
12+
13+
private final SampleBean sampleBean;
14+
15+
public SampleService(SampleBean sampleBean) {
16+
this.sampleBean = sampleBean;
17+
}
18+
19+
public void scheduleNow(Runnable command, long periodSeconds) {
20+
scheduler.scheduleAtFixedRate(command, 0L, periodSeconds, TimeUnit.SECONDS);
21+
}
22+
23+
public String getValue() {
24+
return sampleBean.getValue();
25+
}
26+
27+
// to avoid thread leakage in test execution
28+
@PreDestroy
29+
public void shutdown() {
30+
scheduler.shutdown();
31+
}
32+
}

0 commit comments

Comments
 (0)