Skip to content

Commit b04b852

Browse files
committed
Fixed my issue with my Integration Tests Architectural overhaul. So, I changed it so that the Testcontainers setup will REUSE the same Spring context for each test. This is considered modern and professional. My original setup just destroyed the context and re-created it per test and everything ran smoothly then. But with the more modern change, it broke some tests esp in my GraphQL Integration Tests specifically because of the fact that the context was being created (or at least because some resource was being reused over and over, I'm actually not 100% sure), but I added annotations to clean things up before each test and it's all good now.
1 parent 8d6ccb9 commit b04b852

2 files changed

Lines changed: 7 additions & 72 deletions

File tree

‎springqpro-backend/src/test/java/com/springqprobackend/springqpro/integration/TaskGraphQLIntegrationTest.java‎

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,16 @@
88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.BeforeEach;
1010
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.beans.factory.annotation.Qualifier;
12-
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1311
import org.springframework.boot.test.context.SpringBootTest;
14-
import org.springframework.boot.test.context.TestConfiguration;
1512
import org.springframework.boot.test.web.server.LocalServerPort;
16-
import org.springframework.context.annotation.Bean;
1713
import org.springframework.graphql.test.tester.GraphQlTester;
1814
import org.springframework.graphql.test.tester.HttpGraphQlTester;
19-
import org.springframework.test.context.DynamicPropertyRegistry;
20-
import org.springframework.test.context.DynamicPropertySource;
15+
import org.springframework.test.annotation.DirtiesContext;
16+
import org.springframework.test.context.ActiveProfiles;
2117
import org.springframework.test.web.reactive.server.WebTestClient;
2218
import org.springframework.web.reactive.function.client.ExchangeStrategies;
23-
import org.testcontainers.containers.PostgreSQLContainer;
24-
import org.testcontainers.junit.jupiter.Container;
25-
import org.testcontainers.junit.jupiter.Testcontainers;
2619

2720
import java.time.Instant;
28-
import java.util.Collections;
29-
import java.util.List;
3021
import java.util.concurrent.*;
3122

3223
/* 2025-11-17-NOTE(S)-TO-SELF:
@@ -61,59 +52,13 @@ Test completes (and succeeds) and after the Spring test context begins shutting
6152
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
6253
properties = {
6354
"app.queue.processing.enabled=false",
64-
"spring.task.scheduling.enabled=false",
65-
"spring.main.allow-bean-definition-overriding=true"
55+
"spring.task.scheduling.enabled=false"
6656
}
6757
)
58+
@ActiveProfiles("test")
59+
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
6860
class TaskGraphQLIntegrationTest extends IntegrationTestBase {
6961

70-
// 2025-11-19-DEBUG:+NOTE: ADDED THE TWO CLASSES BELOW TO FIX THE I/O ERRORS I'M GETTING WHERE THREADS PROCESS POST-SHUTDOWN.
71-
// EDIT: THEY DON'T WORK.
72-
/*@TestConfiguration
73-
static class TestConfig {
74-
@Bean
75-
@Qualifier("execService")
76-
public ExecutorService queueExecutorOverride() {
77-
return new DirectExecutorService();
78-
}
79-
@Bean
80-
@Qualifier("schedExec")
81-
public ScheduledExecutorService schedulerOverride() {
82-
return new ScheduledThreadPoolExecutor(1) {
83-
@Override
84-
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
85-
// run immediately in tests
86-
command.run();
87-
return null;
88-
}
89-
90-
@Override
91-
public void shutdown() {}
92-
@Override
93-
public List<Runnable> shutdownNow() { return Collections.emptyList(); }
94-
@Override
95-
public boolean isShutdown() { return false; }
96-
@Override
97-
public boolean isTerminated() { return false; }
98-
@Override
99-
public boolean awaitTermination(long timeout, TimeUnit unit) { return true; }
100-
};
101-
}
102-
}
103-
104-
private static class DirectExecutorService extends AbstractExecutorService {
105-
@Override
106-
public void execute(Runnable command) {
107-
command.run(); // run immediately, same thread.
108-
}
109-
@Override public void shutdown() {}
110-
@Override public List<Runnable> shutdownNow() { return Collections.emptyList(); }
111-
@Override public boolean isShutdown() { return false; }
112-
@Override public boolean isTerminated() { return false; }
113-
@Override public boolean awaitTermination(long timeout, TimeUnit unit) { return true; }
114-
}*/
115-
// 2025-11-19-NOTE:+DEBUG: Removing my Executor overwrites because they're just adding new warnings and stuff (and I get the errors they're meant to prevent anyways).
116-
11762
private GraphQlTester graphQlTester;
11863

11964
@LocalServerPort
@@ -122,18 +67,6 @@ public void execute(Runnable command) {
12267
@Autowired
12368
private TaskRepository taskRepository;
12469

125-
/*@Container
126-
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:18")
127-
.withDatabaseName("springqpro")
128-
.withUsername("springqpro")
129-
.withPassword("springqpro");
130-
131-
@DynamicPropertySource
132-
static void overrideProps(DynamicPropertyRegistry registry) {
133-
registry.add("spring.datasource.url", postgres::getJdbcUrl);
134-
registry.add("spring.datasource.username", postgres::getUsername);
135-
registry.add("spring.datasource.password", postgres::getPassword);
136-
}*/
13770
@BeforeEach
13871
void init() {
13972
taskRepository.deleteAll();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
app.queue.processing.enabled=false
2+
spring.task.scheduling.enabled=false

0 commit comments

Comments
 (0)