Skip to content

Commit

Permalink
docs: add example project with JUnit 5 tests and Bazel integration
Browse files Browse the repository at this point in the history
- Added an example project demonstrating JUnit 5 tests.
- Configured Bazel build files for running tests with JUnit 5.
  • Loading branch information
gibiw committed Feb 14, 2025
1 parent 625f6f6 commit 1067679
Show file tree
Hide file tree
Showing 15 changed files with 2,103 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/junit5/junit5-bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test --jvmopt="--add-opens=java.base/java.lang=ALL-UNNAMED" --jvmopt="-Djunit.jupiter.extensions.autodetection.enabled=true"
1 change: 1 addition & 0 deletions examples/junit5/junit5-bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.0
41 changes: 41 additions & 0 deletions examples/junit5/junit5-bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("//:junit5.bzl", "java_junit5_test")
load("@rules_java//java:defs.bzl", "java_library")

java_library(
name="test-lib",
srcs = glob(["src/test/java/**/*.java"]),
data = glob([
"qase.config.json", # copy config to Bazsel sandbox
]),
resources = glob([
"src/test/java/resources/**/*",
]),
visibility = ["//visibility:public"],
deps= [
"@maven//:org_junit_jupiter_junit_jupiter_api",
"@maven//:org_junit_jupiter_junit_jupiter_params",
"@maven//:org_junit_jupiter_junit_jupiter_engine",
"@maven//:io_qase_qase_junit5_reporter",
"@maven//:io_qase_qase_java_commons",
"@maven//:org_aspectj_aspectjweaver",
"@maven//:org_aspectj_aspectjrt",
],
)

java_junit5_test(
name = "junit5-test",
srcs = glob(["src/test/java/**/*.java"]),
test_packages = ["example"],
deps = [
":test-lib",
"@maven//:io_qase_qase_junit5_reporter",
"@maven//:io_qase_qase_java_commons",
"@maven//:org_aspectj_aspectjweaver",
"@maven//:org_aspectj_aspectjrt",
],
runtime_deps = [],
env = {
# Applies aspects for the correct operation of steps in tests. Specify the path for your local repository.
"JAVA_TOOL_OPTIONS": "-javaagent:/Users/gda/.m2/repository/org/aspectj/aspectjweaver/1.9.22/aspectjweaver-1.9.22.jar",
},
)
6 changes: 6 additions & 0 deletions examples/junit5/junit5-bazel/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
1,630 changes: 1,630 additions & 0 deletions examples/junit5/junit5-bazel/MODULE.bazel.lock

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions examples/junit5/junit5-bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Junit5-Bazel Example

This is a sample project demonstrating how to write and execute tests using the Junit5 framework with Bazel.

## Prerequisites

Ensure that the following tools are installed on your machine:

1. [Java 8](https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html)
2. [Bazel](https://bazel.build/versions/7.2.0/install)

## Setup Instructions

1. Clone this repository by running the following command:
```bash
git clone https://github.com/qase-tms/qase-java.git
cd qase-java/examples/junit5/junit5-bazel
```

2. Create a `qase.config.json` file in the root of the project. You can follow the instructions
on [how to configure the file](https://github.com/qase-tms/qase-java/tree/main/qase-java-commons#readme).

3. Once the configuration is done, run the tests and upload the results to Qase by executing:
```bash
bazel test //...
```

## Additional Resources

For more details on how to use this integration with Qase Test Management, visit
the [Qase Java documentation](https://github.com/qase-tms/qase-java).
31 changes: 31 additions & 0 deletions examples/junit5/junit5-bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "4.3"
RULES_JVM_EXTERNAL_SHA = "6274687f6fc5783b589f56a2f1ed60de3ce1f99bc4e8f9edef3de43bdf7c6e74"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"org.junit:junit-bom:5.11.2",
"org.junit.jupiter:junit-jupiter-api:5.11.2",
"org.junit.jupiter:junit-jupiter-params:5.11.2",
"org.junit.jupiter:junit-jupiter-engine:5.11.2",
"org.junit.platform:junit-platform-console-standalone:1.11.4",
"io.qase:qase-junit5-reporter:4.0.3",
"org.aspectj:aspectjweaver:1.9.22",
"org.aspectj:aspectjrt:1.9.22",
"org.slf4j:slf4j-api:1.7.32"
],
repositories = [
"https://repo1.maven.org/maven2",
],
)

36 changes: 36 additions & 0 deletions examples/junit5/junit5-bazel/junit5.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def java_junit5_test(name, srcs, test_packages = [], deps = [], runtime_deps = [], **kwargs):
FILTER_KWARGS = [
"main_class",
"use_testrunner",
"args",
]

for arg in FILTER_KWARGS:
if arg in kwargs.keys():
kwargs.pop(arg)

if len(test_packages) == 0:
fail("must specify non-empty 'test_packages'")

junit_console_args = ["execute"]
for test_package in test_packages:
junit_console_args += ["--select-package", test_package]

junit_console_args += ["--disable-banner", "--fail-if-no-tests"]

native.java_test(
name = name,
srcs = srcs,
use_testrunner = False,
main_class = "org.junit.platform.console.ConsoleLauncher",
args = junit_console_args,
deps = deps + [
"@maven//:org_junit_jupiter_junit_jupiter_api",
"@maven//:org_junit_jupiter_junit_jupiter_params",
"@maven//:org_junit_jupiter_junit_jupiter_engine",
],
runtime_deps = runtime_deps + [
"@maven//:org_junit_platform_junit_platform_console_standalone",
],
**kwargs
)
31 changes: 31 additions & 0 deletions examples/junit5/junit5-bazel/qase.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"mode": "testops",
"fallback": "report",
"debug": true,
"report": {
"driver": "local",
"connection": {
"local": {
"path": "./build/qase-report",
"format": "json"
}
}
},
"testops": {
"api": {
"token": "<token>",
"host": "qase.io"
},
"run": {
"title": "Junit5 test run",
"description": "Junit5 examples",
"complete": true
},
"defect": false,
"project": "<project_code>",
"batch": {
"size": 200
},
"useV2": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package example;

import io.qase.commons.annotation.Step;
import io.qase.junit5.Qase;
import org.junit.jupiter.api.Test;

public class MethodTests {

@Test
public void testWithComment_success() {
Qase.comment("Test with comment success");
System.out.println("Test with comment success");
}

@Test
public void testWithComment_failed() {
Qase.comment("Test with comment failed");
System.out.println("Test with comment failed");
throw new RuntimeException("Test with comment failed");
}

@Test
public void testWithFileAttachment_success() {
Qase.attach("/Users/gda/Downloads/result.json");
System.out.println("Test with attachment success");
}

@Test
public void testWithFileAttachment_failed() {
Qase.attach("/Users/gda/Downloads/result.json");
System.out.println("Test with attachment failed");
throw new RuntimeException("Test with attachment failed");
}

@Test
public void testWithContentAttachment_success() {
Qase.attach("file1.txt", "Content of file", "text/plain");
System.out.println("Test with link attachment success");
}

@Test
public void testWithContentAttachment_failed() {
Qase.attach("file1.txt", "Content of file", "text/plain");
System.out.println("Test with link attachment failed");
throw new RuntimeException("Test with link attachment failed");
}

@Step("Step with attachment")
public void stepWithAttachment() {
Qase.attach("file.txt", "Content of file", "text/plain");
System.out.println("Step with attachment");
}

@Test
public void testWithStepAttachment_success() {
System.out.println("Test with step attachment success");
stepWithAttachment();
}

@Test
public void testWithStepAttachment_failed() {
System.out.println("Test with step attachment failed");
stepWithAttachment();
throw new RuntimeException("Test with step attachment failed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package example;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

public class ParametrisedTests {
static Stream<Arguments> arguments() {
return Stream.of(
Arguments.of("string1"),
Arguments.of("string2"),
Arguments.of("string3")
);
}

@ParameterizedTest
@MethodSource("arguments")
public void testMethod_success1(String data) {
System.out.println("Data is: " + data);
}

@ParameterizedTest
@MethodSource("arguments")
public void testMethod_failed1(String data) {
System.out.println("Data is: " + data);
throw new RuntimeException("Test failed");
}
}
115 changes: 115 additions & 0 deletions examples/junit5/junit5-bazel/src/test/java/example/SimpleTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package example;

import io.qase.commons.annotation.*;
import io.qase.commons.models.annotation.Field;
import org.junit.jupiter.api.Test;

public class SimpleTests {

@Test
public void testWithoutAnnotation_success() {
System.out.println("Test without annotation success");
}

@Test
public void testWithoutAnnotation_filed() {
System.out.println("Test without annotation failed");
throw new RuntimeException("Test without annotation failed");
}

@Test
@QaseId(1)
public void testWithQaseId_success() {
System.out.println("Test with QaseId");
}

@Test
@QaseId(2)
public void testWithQaseId_failed() {
System.out.println("Test with QaseId");
throw new RuntimeException("Test with QaseId failed");
}

@Test
@QaseTitle("Test with title success")
public void testWithTitle_success() {
System.out.println("Test with title success");
}

@Test
@QaseTitle("Test with title failed")
public void testWithTitle_failed() {
System.out.println("Test with title failed");
throw new RuntimeException("Test with title failed");
}


@Test
@QaseFields(value = {
@Field(name = "description", value = "Description of test success"),
@Field(name = "severity", value = "major"),
})
public void testWithFields_success() {
System.out.println("Test with fields success");
}

@Test
@QaseFields(value = {
@Field(name = "description", value = "Description of test failed"),
@Field(name = "severity", value = "major"),
})
public void testWithFields_failed() {
System.out.println("Test with fields failed");
throw new RuntimeException("Test with fields failed");
}

@Test
@QaseIgnore
public void testWithIgnore_success() {
System.out.println("Test with ignore success");
}

@Test
@QaseIgnore
public void testWithIgnore_failed() {
System.out.println("Test with ignore failed");
throw new RuntimeException("Test with ignore failed");
}

@Test
@QaseSuite("Suite 1")
public void testWithSuite_success() {
System.out.println("Test with suite success");
}

@Test
@QaseSuite("Suite 1")
public void testWithSuite_failed() {
System.out.println("Test with suite failed");
throw new RuntimeException("Test with suite failed");
}

@Test
@QaseSuite("Suite 2\tSub suite")
public void testWithMultipleSuites_success() {
System.out.println("Test with multiple suites success");
}

@Test
@QaseSuite("Suite 2\tSub suite")
public void testWithMultipleSuites_failed() {
System.out.println("Test with multiple suites failed");
throw new RuntimeException("Test with multiple suites failed");
}

@Step("Step 1")
private void step01() {
step02();
System.out.println("Step 1");
}

@Step("Step 2")
private void step02() {
System.out.println("Step 2");
}
}
Loading

0 comments on commit 1067679

Please sign in to comment.