Skip to content

Instantly share code, notes, and snippets.

public class ReportingState {
private final Map<String, String> results = new ConcurrentHashMap<>();
public void addResult(String testName, String result) {
results.put(testName, result);
}
public void report() {
System.out.println("---- Test Results ----");
results.forEach((testName, result) -> {
@ExtendWith(ReportingExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
// We need to enable lazy initialization to avoid the EntityManager creation during the context loading
"spring.main.lazy-initialization=true"
})
public class MetricsTests {
@Autowired
TestRestTemplate testRestTemplate;
public class ReportingExtension implements TestWatcher, BeforeEachCallback, AfterAllCallback, ParameterResolver {
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(ReportingExtension.class);
private static final String REPORTING_STATE_KEY = "reportingState";
@Override
public void beforeEach(ExtensionContext context) throws Exception {
// Store the start time for the test
context.getStore(NAMESPACE).put("startTime", System.currentTimeMillis());
}
@ExtendWith({
PostgreSQLExtension.class,
ReportingExtension.class
})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
// We need to enable lazy initialization to avoid the EntityManager creation during the context loading
"spring.main.lazy-initialization=true"
})
class CleanTimingTest {
@Autowired
public class ReportingExtension implements TestWatcher, BeforeEachCallback {
private long startTime;
@Override
public void testDisabled(ExtensionContext context, java.util.Optional<String> reason) {
System.out.println(context.getDisplayName() + " DISABLED" + reason.map(r -> ": " + r).orElse(""));
}
@Override
public void testSuccessful(ExtensionContext context) {
class ManuallyTimingTest {
private long startTime;
@BeforeEach
void startTimer() {
startTime = System.currentTimeMillis();
}
@AfterEach
@ExtendWith(SQLiteExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
// We need to enable lazy initialization to avoid the EntityManager creation during the context loading
"spring.main.lazy-initialization=true"
})
class CleanSQLiteTest {
@Autowired
EntityManager entityManager;
@Test
public class SQLiteExtension implements BeforeAllCallback, AfterAllCallback {
static String uniqueDbName = "memdb" + UUID.randomUUID();
private static final String JDBC_URL = "jdbc:sqlite:file:" + uniqueDbName + "?mode=memory&cache=shared";
private Connection connection;
@Override
public void beforeAll(ExtensionContext context) throws Exception {
// Set system properties
@ExtendWith(PostgreSQLExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
// We need to enable lazy initialization to avoid the EntityManager creation during the context loading
"spring.main.lazy-initialization=true"
})
class CleanPostgreSQLTest {
@Autowired
EntityManager entityManager;
@Test
public class PostgreSQLExtension implements BeforeAllCallback, AfterAllCallback {
private static PostgreSQLContainer<?> postgres;
@Override
@SneakyThrows
public void beforeAll(ExtensionContext context) {
// Start the PostgreSQL container
postgres = new PostgreSQLContainer<>("postgres:latest")
.withDatabaseName("testdb")