Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement JUnit5 support and parameterize tests #4473

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aspect/fast_build_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _fast_build_info_impl(target, ctx):
"launcher": launcher,
"swigdeps": getattr(ctx.rule.attr, "swigdeps", True),
"jvm_flags": getattr(ctx.rule.attr, "jvm_flags", []),
"main_class": getattr(ctx.rule.attr, "main_class", None),
}
annotation_processing = target[JavaInfo].annotation_processing
if annotation_processing:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,27 @@ fast_build_aspect_test_fixture(
deps = [":FooTestWithCustomLauncher"],
)

java_test(
name = "FooTestWithCustomMainClass",
srcs = ["FooTest.java"],
main_class = "com.google.idea.blaze.aspect.java.javatest.fake.TestRunner",
tags = ["manual"], # This test is not meant to be ran on its own. The main class represents a custom test runner.
test_class = "com.google.idea.blaze.aspect.java.javatest.FooTest",
deps = ["@junit//jar"],
)

fast_build_aspect_test_fixture(
name = "footest_with_custom_main_class_fast_build_fixture",
deps = [":FooTestWithCustomMainClass"],
)

java_test(
name = "JavaTestFastBuildAspectTest",
srcs = ["JavaTestFastBuildAspectTest.java"],
data = [
":footest_no_launcher_fast_build_fixture",
":footest_with_custom_launcher_fast_build_fixture",
":footest_with_custom_main_class_fast_build_fixture",
],
deps = [
"//aspect/testing:guava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public void testCustomJavaLauncher() throws Exception {
.isEqualTo(aspectLoader.testRelative(":custom_java_launcher"));
}

@Test
public void testCustomMainClass() throws Exception {
FastBuildAspectTestFixture fixture =
aspectLoader.loadTestFixture(":footest_with_custom_main_class_fast_build_fixture");
FastBuildBlazeData data =
getDataForTarget(aspectLoader.testRelative(":FooTestWithCustomMainClass"), fixture);
assertThat(data.getJavaInfo().getMainClass())
.isEqualTo("com.google.idea.blaze.aspect.java.javatest.fake.TestRunner");
}


private FastBuildBlazeData getDataForTarget(String target, FastBuildAspectTestFixture fixture) {
return fixture.getTargetList().stream()
Expand Down
Empty file.
27 changes: 27 additions & 0 deletions examples/java/greetings_project/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

jvm_maven_import_external(
name = "junit",
Expand All @@ -7,3 +8,29 @@ jvm_maven_import_external(
licenses = ["notice"], # Common Public License 1.0
server_urls = ["https://repo1.maven.org/maven2"],
)

# JUnit 5

load("//:junit5_deps.bzl", "instantiate_junit5_deps")

instantiate_junit5_deps()

# `contrib_rules_jvm` gives us access to `java_test_suite` and the JUnit5 runner.
CONTRIB_RULES_JVM_VERSION = "0.9.0"

CONTRIB_RULES_JVM_SHA = "548f0583192ff79c317789b03b882a7be9b1325eb5d3da5d7fdcc4b7ca69d543"

http_archive(
name = "contrib_rules_jvm",
sha256 = CONTRIB_RULES_JVM_SHA,
strip_prefix = "rules_jvm-%s" % CONTRIB_RULES_JVM_VERSION,
url = "https://github.com/bazel-contrib/rules_jvm/archive/refs/tags/v%s.tar.gz" % CONTRIB_RULES_JVM_VERSION,
)

load("@contrib_rules_jvm//:repositories.bzl", "contrib_rules_jvm_deps")

contrib_rules_jvm_deps()

load("@contrib_rules_jvm//:setup.bzl", "contrib_rules_jvm_setup")

contrib_rules_jvm_setup()
47 changes: 44 additions & 3 deletions examples/java/greetings_project/greeting_lib/BUILD
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test", "java_test_suite")
load("//:junit5_deps.bzl", "JUNIT5_DEPS")

java_library(
name = "greeting_lib",
srcs = ["src/com/example/Greeting.java"],
visibility = ["//visibility:public"],
)

java_test(
name = "greeting_test",
srcs = ["tests/com/example/GreetingTest.java"],
test_class = "com.example.GreetingTest",
name = "greeting_test_junit4",
srcs = [
"tests/com/example/junit4/GreetingTest.java",
],
test_class = "com.example.junit4.GreetingTest",
deps = [
":greeting_lib",
"@junit//jar",
],
)

java_test(
name = "parameterized_greeting_test_junit4",
srcs = [
"tests/com/example/junit4/ParameterizedGreetingTest.java",
],
test_class = "com.example.junit4.ParameterizedGreetingTest",
deps = [
":greeting_lib",
"@junit//jar",
],
)

java_junit5_test(
name = "greeting_test_junit5",
size = "small",
srcs = [
"tests/com/example/junit5/GreetingTest.java",
],
test_class = "com.example.junit5.GreetingTest",
deps = [
":greeting_lib",
] + JUNIT5_DEPS,
)

java_junit5_test(
name = "parameterized_greeting_test_junit5",
size = "small",
srcs = [
"tests/com/example/junit5/ParameterizedGreetingTest.java",
],
test_class = "com.example.junit5.ParameterizedGreetingTest",
deps = [
":greeting_lib",
] + JUNIT5_DEPS,
)

java_test(
name = "env_vars_test",
srcs = ["tests/com/example/EnvVarsTest.java"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example;
package com.example.junit4;

import org.junit.Test;
import com.example.Greeting;

import java.util.Arrays;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.junit4;

import com.example.Greeting;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class ParameterizedGreetingTest {
@Parameterized.Parameter
public String name;

@Parameterized.Parameters(name = "Test name {0}")
public static String[] names() {
return new String[]{"Henry", "Roberta"};
}

@Test
public void testGreetOneName() {
List<String> got = Greeting.getGreetings("greetings", Collections.singletonList(name));
assertEquals(1, got.size());
String expected = "greetings " + name + "!";
assertEquals(expected, got.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.junit5;

import com.example.Greeting;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GreetingTest {
@Test
public void testGreetNoNames() {
List<String> got = Greeting.getGreetings("greetings", Collections.emptyList());
assertEquals(0, got.size());
}

@Test
public void testGreetOneName() {
List<String> got = Greeting.getGreetings("greetings", Collections.singletonList("name"));
assertEquals(1, got.size());
assertEquals("greetings name!", got.get(0));
}

@Test
public void testGreetMultipleNames() {
List<String> got = Greeting.getGreetings("greetings", Arrays.asList("name1", "name2", "name3"));
assertEquals(3, got.size());
assertEquals("greetings name1!", got.get(0));
assertEquals("greetings name2!", got.get(1));
assertEquals("greetings name3!", got.get(2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.junit5;

import com.example.Greeting;

import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParameterizedGreetingTest {
@ParameterizedTest
@ValueSource(strings = { "Henry", "Roberta" })
public void testGreetOneName(String name) {
List<String> got = Greeting.getGreetings("greetings", Collections.singletonList(name));
assertEquals(1, got.size());
String expected = "greetings " + name + "!";
assertEquals(expected, got.get(0));
}
}
60 changes: 60 additions & 0 deletions examples/java/greetings_project/junit5_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")

JUNIT_JUPITER_VERSION = "5.9.2"

JUNIT_PLATFORM_VERSION = "1.9.2"

_JUNIT5_DEPS_TO_FETCH = [
{
"name": "junit_platform_launcher",
"coords": "org.junit.platform:junit-platform-launcher:%s" % JUNIT_PLATFORM_VERSION,
"artifact_sha256": "eef139eb09c98e9cdd358b6ce4c6cdd59b30c6a88096e369c33ba96e67edf0e4",
},
{
"name": "junit_platform_reporting",
"coords": "org.junit.platform:junit-platform-reporting:%s" % JUNIT_PLATFORM_VERSION,
"artifact_sha256": "d6788db1c941c1247e07d8104f57c3f06175cadfd43060a792493fe9195671db",
},
{
"name": "junit_platform_commons",
"coords": "org.junit.platform:junit-platform-commons:%s" % JUNIT_PLATFORM_VERSION,
"artifact_sha256": "624a3d745ef1d28e955a6a67af8edba0fdfc5c9bad680a73f67a70bb950a683d",
},
{
"name": "junit_platform_engine",
"coords": "org.junit.platform:junit-platform-engine:%s" % JUNIT_PLATFORM_VERSION,
"artifact_sha256": "25f23dc535a091e9dc80c008faf29dcb92be902e6911f77a736fbaf019908367",
},
{
"name": "junit_jupiter_api",
"coords": "org.junit.jupiter:junit-jupiter-api:%s" % JUNIT_JUPITER_VERSION,
"artifact_sha256": "f767a170f97127b0ad3582bf3358eabbbbe981d9f96411853e629d9276926fd5",
},
{
"name": "junit_jupiter_params",
"coords": "org.junit.jupiter:junit-jupiter-params:%s" % JUNIT_JUPITER_VERSION,
"artifact_sha256": "bde91900a5ce5d6663bb44bc708494b35daefcd73e1bb7afa61a4affe38ea97d",
},
{
"name": "junit_jupiter_engine",
"coords": "org.junit.jupiter:junit-jupiter-engine:%s" % JUNIT_JUPITER_VERSION,
"artifact_sha256": "74cfc49388f760413ff348ca2c9ab39527484b57deecd157f2275a5f8a5fe971",
},
{
"name": "opentest4j",
"coords": "org.opentest4j:opentest4j:1.2.0",
"artifact_sha256": "58812de60898d976fb81ef3b62da05c6604c18fd4a249f5044282479fc286af2",
},
]

JUNIT5_DEPS = ["@{name}//jar".format(name = dep["name"]) for dep in _JUNIT5_DEPS_TO_FETCH]

def instantiate_junit5_deps():
for dep in _JUNIT5_DEPS_TO_FETCH:
jvm_maven_import_external(
name = dep["name"],
artifact = dep["coords"],
artifact_sha256 = dep["artifact_sha256"],
licenses = ["notice"], # Common Public License 1.0
server_urls = ["https://repo1.maven.org/maven2"],
)
Loading
Loading