Skip to content

Commit 1f25d6b

Browse files
committed
Improving tests
1 parent c232e68 commit 1f25d6b

File tree

7 files changed

+1348
-1088
lines changed

7 files changed

+1348
-1088
lines changed

examples/spring-boot-demo/implementation/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<version>3.5.2</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
11-
<groupId>com.example</groupId>
12-
<artifactId>demo</artifactId>
11+
<groupId>info.jab.ms</groupId>
12+
<artifactId>sakila-api</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<properties>
1515
<java.version>24</java.version>

examples/spring-boot-demo/implementation/src/main/java/info/jab/ms/controller/GlobalExceptionHandler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public ResponseEntity<ProblemDetail> handleRuntimeException(
5151
);
5252

5353
problemDetail.setTitle("Internal Server Error");
54-
problemDetail.setInstance(URI.create(request.getRequestURI()));
54+
55+
// Handle null request URI gracefully
56+
String requestUri = request.getRequestURI();
57+
problemDetail.setInstance(URI.create(requestUri != null ? requestUri : "/unknown"));
58+
5559
problemDetail.setProperty("timestamp", Instant.now());
5660
problemDetail.setProperty("errorId", errorId);
5761

@@ -75,7 +79,11 @@ public ResponseEntity<ProblemDetail> handleGenericException(
7579
);
7680

7781
problemDetail.setTitle("Internal Server Error");
78-
problemDetail.setInstance(URI.create(request.getRequestURI()));
82+
83+
// Handle null request URI gracefully
84+
String requestUri = request.getRequestURI();
85+
problemDetail.setInstance(URI.create(requestUri != null ? requestUri : "/unknown"));
86+
7987
problemDetail.setProperty("timestamp", Instant.now());
8088
problemDetail.setProperty("errorId", errorId);
8189

examples/spring-boot-demo/implementation/src/main/java/info/jab/ms/repository/FilmRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@Repository
1919
public interface FilmRepository extends ListCrudRepository<Film, Integer> {
2020

21+
//TDDO: Remove collate
2122
/**
2223
* Task 8.4: Implement findByTitleStartingWith(String prefix) method
2324
*
@@ -27,14 +28,15 @@ public interface FilmRepository extends ListCrudRepository<Film, Integer> {
2728
* @param prefix The starting letters to search for (case-insensitive)
2829
* @return List of films with titles starting with the prefix
2930
*/
30-
@Query("SELECT film_id, title FROM film WHERE UPPER(title) LIKE UPPER(:prefix || '%') ORDER BY title")
31+
@Query("SELECT film_id, title FROM film WHERE UPPER(title) LIKE UPPER(:prefix || '%') ORDER BY title COLLATE \"C\"")
3132
List<Film> findByTitleStartingWith(@Param("prefix") String prefix);
3233

34+
//TDDO: Remove collate
3335
/**
3436
* Alternative method to find all films (for when no filter is applied)
3537
*
3638
* @return List of all films ordered by title
3739
*/
38-
@Query("SELECT film_id, title FROM film ORDER BY title")
40+
@Query("SELECT film_id, title FROM film ORDER BY title COLLATE \"C\"")
3941
List<Film> findAllOrderByTitle();
4042
}
Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package info.jab.ms;
22

33
import info.jab.ms.common.PostgreSQLTestBase;
4+
import org.junit.jupiter.api.DisplayName;
45
import org.junit.jupiter.api.Test;
56
import org.springframework.boot.test.context.SpringBootTest;
67

8+
import static org.assertj.core.api.Assertions.assertThat;
9+
710
/**
8-
* Basic integration tests for the Demo Application.
11+
* Integration tests for the Demo Application.
12+
*
13+
* Following Java Unit Testing Guidelines:
14+
* - Given-When-Then structure
15+
* - Descriptive test names using should_ExpectedBehavior_when_StateUnderTest pattern
16+
* - AssertJ for fluent assertions
17+
* - Clear test documentation
918
*
1019
* This class extends PostgreSQLTestBase to inherit the common TestContainer setup
1120
* with Sakila schema and test data. It provides basic smoke tests to verify
1221
* the Spring Boot application context loads correctly with the database.
1322
*/
1423
@SpringBootTest
24+
@DisplayName("Main Application Integration Tests")
1525
class MainApplicationTests extends PostgreSQLTestBase {
1626

1727
/**
@@ -22,19 +32,77 @@ class MainApplicationTests extends PostgreSQLTestBase {
2232
* - Spring Boot application context loads without errors
2333
* - Database connection is established via @ServiceConnection
2434
* - All auto-configuration works properly
35+
* - TestContainer infrastructure is working correctly
2536
*/
2637
@Test
27-
void contextLoads() {
28-
// This test will load the Spring context with the TestContainer database
29-
// The @ServiceConnection annotation in the base class automatically configures
30-
// the database connection properties, so no manual configuration is needed
38+
@DisplayName("Should load Spring context successfully with TestContainer database")
39+
void should_loadSpringContextSuccessfully_when_testContainerDatabaseIsAvailable() {
40+
// Given - TestContainer PostgreSQL is started and Spring context is loaded
41+
42+
// When - We check the container status and connectivity
43+
var container = getPostgresContainer();
44+
45+
// Then - Container should be running and properly configured
46+
assertThat(container.isRunning())
47+
.as("PostgreSQL container should be running")
48+
.isTrue();
3149

32-
// Verify the container is running (inherited from base class)
33-
assert getPostgresContainer().isRunning() : "PostgreSQL container should be running";
34-
assert getPostgresContainer().getDatabaseName().equals("testdb") : "Database should be testdb";
50+
assertThat(container.getDatabaseName())
51+
.as("Database name should match test configuration")
52+
.isEqualTo("testdb");
53+
54+
assertThat(container.getUsername())
55+
.as("Username should match test configuration")
56+
.isEqualTo("testuser");
57+
58+
// And - Connection properties should be valid
59+
assertThat(container.getJdbcUrl())
60+
.as("JDBC URL should be properly formatted")
61+
.isNotBlank()
62+
.startsWith("jdbc:postgresql://")
63+
.contains("testdb");
64+
65+
// And - Container should have a valid ID indicating it's running
66+
assertThat(container.getContainerId())
67+
.as("Container should have valid ID")
68+
.isNotBlank();
3569

3670
// Context loading success implies database connectivity is working
37-
System.out.println("Spring Boot context loaded successfully with TestContainer database");
38-
System.out.println("Database URL: " + getPostgresContainer().getJdbcUrl());
71+
// The @ServiceConnection annotation automatically configures the database connection
72+
System.out.println("✅ Spring Boot context loaded successfully with TestContainer database");
73+
System.out.println("📊 Database URL: " + container.getJdbcUrl());
74+
System.out.println("🐘 PostgreSQL Container ID: " + container.getContainerId());
75+
}
76+
77+
@Test
78+
@DisplayName("Should have database connectivity working correctly")
79+
void should_haveDatabaseConnectivityWorking_when_applicationStarts() {
80+
// Given - Application context is loaded with TestContainer
81+
82+
// When - We verify basic database connectivity indicators
83+
var container = getPostgresContainer();
84+
85+
// Then - Database should be accessible
86+
assertThat(container.isCreated())
87+
.as("Container should be created")
88+
.isTrue();
89+
90+
assertThat(container.isRunning())
91+
.as("Container should be running and accessible")
92+
.isTrue();
93+
94+
// And - Standard PostgreSQL port should be mapped
95+
assertThat(container.getMappedPort(5432))
96+
.as("PostgreSQL port should be mapped")
97+
.isPositive();
98+
99+
// And - Container logs should not contain critical errors
100+
String logs = container.getLogs();
101+
assertThat(logs)
102+
.as("Container logs should indicate successful startup")
103+
.contains("database system is ready to accept connections");
104+
105+
System.out.println("✅ Database connectivity verified");
106+
System.out.println("🔌 Mapped PostgreSQL port: " + container.getMappedPort(5432));
39107
}
40108
}

0 commit comments

Comments
 (0)