11package info .jab .ms ;
22
33import info .jab .ms .common .PostgreSQLTestBase ;
4+ import org .junit .jupiter .api .DisplayName ;
45import org .junit .jupiter .api .Test ;
56import 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" )
1525class 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