-
Notifications
You must be signed in to change notification settings - Fork 0
JUnit5 Integration
Frody edited this page Sep 3, 2026
·
1 revision
OpenApiGuard provides a first-class JUnit 5 extension that integrates security regression testing directly into standard Maven and Gradle build cycles.
Annotate test classes or test methods to execute the security engine:
import io.openapiguard.junit5.ApiSecConfig;
import io.openapiguard.junit5.ApiSecTest;
import io.openapiguard.core.report.SecurityReport;
import org.junit.jupiter.api.Test;
@ApiSecConfig("classpath:security-config.yaml")
class ApiSecurityAuditTest {
@Test
@ApiSecTest(failLevel = "HIGH")
void shouldHaveNoHighSeverityVulnerabilities(SecurityReport report) {
// Automatically fails if any findings of severity HIGH or CRITICAL are detected
assert report.totalFindings() == 0;
}
}The failLevel parameter specifies the minimum severity that triggers test failure:
-
"CRITICAL": Only fails if critical vulnerabilities (BOLA, BFLA) are detected. -
"HIGH": Fails on high and critical vulnerabilities (default). -
"MEDIUM": Fails on medium, high, and critical issues. -
"LOW": Strict zero-tolerance gate.
Execute OpenApiGuard against a live Spring Boot server spun up on a random port:
import io.openapiguard.core.ApiSecSuite;
import io.openapiguard.core.report.SecurityReport;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SpringBootApiSecurityTest {
@LocalServerPort
private int port;
@Test
void runSecuritySuiteAgainstRunningServer() {
SecurityReport report = ApiSecSuite.builder()
.target("http://localhost:" + port)
.openApi("classpath:openapi.yaml")
.enableDefaults()
.build()
.execute();
report.assertNoCriticalVulnerabilities();
}
}OpenApiGuard can export findings in SARIF (Static Analysis Results Interchange Format). When uploaded in GitHub Actions, vulnerabilities appear directly on pull requests and in GitHub's Security → Code scanning alerts tab:
- name: Run OpenApiGuard Security Tests
run: mvn test -Dtest=ApiSecurityAuditTest
- name: Upload SARIF Report to GitHub Code Scanning
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: target/openapiguard-report.sarifOpenApiGuard • Contract-Driven Security Testing for REST APIs • Licensed under Apache-2.0