Skip to content
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

PostgreSQL: Demonstrate integration-tests module with Testcontainers #268

Closed
wants to merge 5 commits into from
Closed
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
81 changes: 81 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>my</groupId>
<artifactId>bookshop-parent</artifactId>
<version>${revision}</version>
</parent>

<artifactId>bookshop-integration-tests</artifactId>

<name>bookshop-integration-tests</name>

<dependencies>
<dependency>
<groupId>my</groupId>
<artifactId>bookshop</artifactId>
<version>${revision}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failIfNoTests>false</failIfNoTests>
</configuration>
</plugin>

<plugin>
<!-- Tests ending with *IT.java will be executed here -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package my.bookshop.it;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import my.bookshop.Application;

@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
@Testcontainers
class SimpleIntegrationIT {

@Autowired
MockMvc client;

@Container
private static PostgreSQLContainer<?> pgContainer = new PostgreSQLContainer<>("postgres:14");

@DynamicPropertySource
static void registerPosgreSQLProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", pgContainer::getJdbcUrl);
registry.add("spring.datasource.username", pgContainer::getUsername);
registry.add("spring.datasource.password", pgContainer::getPassword);
}

@Test
void booksAreReadable() throws Exception {
client.perform(get("/api/browse/Books").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}

@Test
@WithMockUser("admin")
void booksAreManageable() throws Exception {
client.perform(get("/api/admin/Books").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}

@Test
void booksAreProtected() throws Exception {
client.perform(get("/api/admin/Books").accept(MediaType.APPLICATION_JSON)).andExpect(status().isUnauthorized());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<modules>
<module>srv</module>
<module>integration-tests</module>
</modules>

<dependencyManagement>
Expand Down
Loading