fix: Fix test stability issues

This commit is contained in:
Ilkka Seppälä committed 2026-06-07 12:10:39 +03:00
1 parent 13cde46f2e
commit 177dc52704
24 files changed
+253 -111

No files matched your search

@@ -40,6 +40,11 @@ public class App {
* @param args command line args.
*/
public static void main(String[] args) {
SpringApplication.run(App.class, args);
var context = SpringApplication.run(App.class, args);
if (args.length > 0 && "test".equals(args[0])) {
// Close the context immediately during tests to prevent Tomcat/background threads from
// hanging the JVM
context.close();
}
}
}
@@ -44,9 +44,22 @@ public class DataSourceService {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
/** Constructor & Scheduler to push random data. */
@org.springframework.beans.factory.annotation.Autowired
public DataSourceService(DataRepository repository) {
this(repository, true);
}
/**
* Constructor with option to start the scheduler.
*
* @param repository the data repository
* @param startScheduler true to start the data generation scheduler
*/
public DataSourceService(DataRepository repository, boolean startScheduler) {
this.repository = repository;
scheduleDataGeneration();
if (startScheduler) {
scheduleDataGeneration();
}
}
private void scheduleDataGeneration() {
@@ -78,4 +91,9 @@ public class DataSourceService {
public Map<Integer, String> getAllData() {
return repository.findAll();
}
@javax.annotation.PreDestroy
public void shutdown() {
scheduler.shutdown();
}
}
@@ -28,12 +28,16 @@ import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.test.context.EmbeddedKafka;
@SpringBootTest
@EmbeddedKafka(
partitions = 1,
topics = {"updates", "API"})
public class AppTest {
@Test
void polling() {
assertDoesNotThrow(() -> App.main(new String[] {}));
assertDoesNotThrow(() -> App.main(new String[] {"test"}));
}
}
@@ -38,7 +38,7 @@ public class DataSourceServiceTest {
@BeforeEach
void setUp() {
repository = new DataRepository();
service = new DataSourceService(repository);
service = new DataSourceService(repository, false);
}
@Test
@@ -77,8 +77,8 @@ public class DataSourceServiceTest {
Map<Integer, String> result1 = service.getAllData();
assertEquals(size + 2, result.size(), "Should return all stored data.");
assertEquals("First", result.get(1), "Value for key 1 should be 'First'.");
assertEquals("Second", result.get(2), "Value for key 2 should be 'Second'.");
assertEquals(size + 2, result1.size(), "Should return all stored data.");
assertEquals("First", result1.get(1), "Value for key 1 should be 'First'.");
assertEquals("Second", result1.get(2), "Value for key 2 should be 'Second'.");
}
}
@@ -32,6 +32,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
var context = SpringApplication.run(App.class, args);
if (args.length > 0 && "test".equals(args[0])) {
// Close the context immediately during tests to prevent Tomcat/background threads from
// hanging the JVM
context.close();
}
}
}
@@ -29,12 +29,16 @@ import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.test.context.EmbeddedKafka;
@SpringBootTest
@EmbeddedKafka(
partitions = 1,
topics = {"updates", "API"})
public class AppTest {
@Test
void subscriber() {
assertDoesNotThrow(() -> App.main(new String[] {}));
assertDoesNotThrow(() -> App.main(new String[] {"test"}));
}
}