Skip to content

Commit 5f8a40e

Browse files
Merge pull request #34 from eliasnogueira/junit-extension
Junit extension
2 parents 3d18e63 + b871dfc commit 5f8a40e

File tree

7 files changed

+102
-83
lines changed

7 files changed

+102
-83
lines changed

.github/workflows/test-execution.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ jobs:
4444
- name: Build with Maven
4545
run: mvn -q -B -DskipTests package --file pom.xml
4646

47-
- name: Run Health Check Tests
48-
run: mvn -q -B test -Dgroups="health"
49-
5047
- name: Run Contract Tests
5148
run: mvn -q -B test -Dgroups="contract"
5249

CHANGELOG.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.8.0] - 12-01-2026
9+
10+
### Added
11+
12+
- Thread-safe caching JUnit Extension mechanism in `EnvironmentAvailabilityExtension` to ensure the health check runs
13+
only once per test suite execution.
14+
815
## [2.7.0] - 04-01-2026
916

1017
### Changed
1118

1219
- Adoption of Java 25
13-
- Set `java.version` to 25
14-
- Changed `actions/setup-java@v4` in `.github/workflows/test-execution.yml` to Java 25
20+
- Set `java.version` to 25
21+
- Changed `actions/setup-java@v4` in `.github/workflows/test-execution.yml` to Java 25
1522
- Renamed `MessageFormat` to `LocationUrlResolver` and updated usages
1623
- Updated CPF generation to avoid deprecated DataFaker API
1724
- Updated the following dependencies
18-
- `maven-compiler-plugin.version -> 3.14.1`
19-
- `maven-surefire-plugin.version -> 3.5.4`
20-
- `restassured.version -> 6.0.0`
21-
- `junit.jupiter.version -> 6.0.1`
22-
- `assertj.version -> 3.27.6`
23-
- `datafaker.version -> 2.5.3`
24-
- `log4j.version -> 2.25.3`
25-
- `slf4j.version -> 2.0.17`
26-
- `allure.version -> 2.32.0`
27-
- `allure-maven.version -> 2.17.0`
28-
- `aspectj.version -> 1.9.25.1`
29-
- `commons-codec.version -> 1.20.0`
30-
- `jackson-databind.version -> 3.0.3`
31-
- `rhino.version -> 1.9.0`
25+
- `maven-compiler-plugin.version -> 3.14.1`
26+
- `maven-surefire-plugin.version -> 3.5.4`
27+
- `restassured.version -> 6.0.0`
28+
- `junit.jupiter.version -> 6.0.1`
29+
- `assertj.version -> 3.27.6`
30+
- `datafaker.version -> 2.5.3`
31+
- `log4j.version -> 2.25.3`
32+
- `slf4j.version -> 2.0.17`
33+
- `allure.version -> 2.32.0`
34+
- `allure-maven.version -> 2.17.0`
35+
- `aspectj.version -> 1.9.25.1`
36+
- `commons-codec.version -> 1.20.0`
37+
- `jackson-databind.version -> 3.0.3`
38+
- `rhino.version -> 1.9.0`
3239
- Added `junit-platform-launcher` (test scope)
3340
- Removed `allure-testng`
3441

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
<groupId>org.junit.platform</groupId>
9595
<artifactId>junit-platform-launcher</artifactId>
9696
<version>${junit.jupiter.version}</version>
97-
<scope>test</scope>
9897
</dependency>
9998

10099
<dependency>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2026 Elias Nogueira
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.eliasnogueira.credit.extensions;
25+
26+
import com.eliasnogueira.credit.config.ConfigurationManager;
27+
import io.restassured.RestAssured;
28+
import org.apache.http.HttpStatus;
29+
import org.apache.logging.log4j.LogManager;
30+
import org.apache.logging.log4j.Logger;
31+
import org.junit.jupiter.api.Assumptions;
32+
import org.junit.jupiter.api.extension.BeforeAllCallback;
33+
import org.junit.jupiter.api.extension.ExtensionContext;
34+
35+
import static io.restassured.config.ConnectionConfig.connectionConfig;
36+
37+
public class EnvironmentAvailabilityExtension implements BeforeAllCallback {
38+
39+
private static Boolean isEnvironmentReady;
40+
41+
private static final Logger log = LogManager.getLogger(EnvironmentAvailabilityExtension.class);
42+
43+
@Override
44+
public void beforeAll(ExtensionContext context) {
45+
if (isEnvironmentReady == null) checkEnvironmentHealth();
46+
47+
Assumptions.assumeTrue(isEnvironmentReady, "Environment is not available.");
48+
}
49+
50+
private synchronized void checkEnvironmentHealth() {
51+
if (isEnvironmentReady != null) return;
52+
53+
try {
54+
RestAssured.given()
55+
.baseUri(buildHealthUrlString())
56+
.config(RestAssured.config().connectionConfig(connectionConfig()
57+
.closeIdleConnectionsAfterEachResponse()))
58+
.when().get().then().statusCode(HttpStatus.SC_OK);
59+
60+
isEnvironmentReady = true;
61+
} catch (Exception e) {
62+
log.error("Environment is not available. Check it before running the tests.");
63+
log.error("Endpoint used: {}", buildHealthUrlString());
64+
log.error("Exception: {}", e.getMessage());
65+
isEnvironmentReady = false;
66+
}
67+
}
68+
69+
private String buildHealthUrlString() {
70+
var config = ConfigurationManager.getConfiguration();
71+
return String.format("%s:%s%s/health", config.baseURI(), config.port(), config.health());
72+
}
73+
}

src/test/java/com/eliasnogueira/credit/BaseAPI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
import com.eliasnogueira.credit.config.Configuration;
2727
import com.eliasnogueira.credit.config.ConfigurationManager;
28+
import com.eliasnogueira.credit.extensions.EnvironmentAvailabilityExtension;
2829
import io.restassured.RestAssured;
2930
import io.restassured.config.SSLConfig;
3031
import io.restassured.filter.log.RequestLoggingFilter;
3132
import io.restassured.filter.log.ResponseLoggingFilter;
3233
import io.restassured.path.json.config.JsonPathConfig.NumberReturnType;
3334
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.extension.ExtendWith;
3436

3537
import static io.restassured.RestAssured.basePath;
3638
import static io.restassured.RestAssured.baseURI;
@@ -39,6 +41,7 @@
3941
import static io.restassured.config.JsonConfig.jsonConfig;
4042
import static io.restassured.config.RestAssuredConfig.newConfig;
4143

44+
@ExtendWith(EnvironmentAvailabilityExtension.class)
4245
public abstract class BaseAPI {
4346

4447
protected static Configuration configuration;

src/test/java/com/eliasnogueira/credit/e2e/FullSimulationE2ETest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525

2626
import com.eliasnogueira.credit.client.RestrictionsClient;
2727
import com.eliasnogueira.credit.client.SimulationsClient;
28+
import com.eliasnogueira.credit.extensions.EnvironmentAvailabilityExtension;
2829
import org.junit.jupiter.api.DisplayName;
2930
import org.junit.jupiter.api.Tag;
3031
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.extension.ExtendWith;
3133

3234
import static com.eliasnogueira.credit.data.changeless.TestSuiteTags.E2E;
3335
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
3436
import static org.assertj.core.api.Assertions.assertThat;
3537

38+
@ExtendWith(EnvironmentAvailabilityExtension.class)
3639
class FullSimulationE2ETest {
3740

3841
private final RestrictionsClient restrictionsClient = new RestrictionsClient();

src/test/java/com/eliasnogueira/credit/general/CreditHealthCheckTest.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)