Skip to content

Instantly share code, notes, and snippets.

# Unzip the Fat JAR
RUN mkdir -p target/extracted && \
java -Djarmode=layertools -jar target/*.jar extract --destination target/extracted
# Copy layers individually for maximum caching
COPY --from=build /app/target/extracted/dependencies/ ./
COPY --from=build /app/target/extracted/spring-boot-loader/ ./
COPY --from=build /app/target/extracted/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService; // Service must be mocked in a slice test
@Test
@Testcontainers
@SpringBootTest
class MyIntegrationTest {
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
@Test
void testDatabaseLogic() {
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService; // This mock is injected into UserController
@Test
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Test
void testHelloWorldEndpoint() {
webTestClient.get().uri("/api/hello")
.exchange()
dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
server:
shutdown: graceful
spring:
lifecycle:
timeout-per-shutdown-phase: 30s
databaseChangeLog:
- changeSet:
id: 1
author: gemini
changes:
- createTable:
tableName: users
columns:
- column:
name: id
CREATE TABLE users (
id BIGINT PRIMARY KEY,
username VARCHAR(255) NOT NULL
);