Skip to content

Commit

Permalink
Implement JUnit5 support and parameterize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja Lorente committed Feb 20, 2023
1 parent deb79cf commit a5115d5
Show file tree
Hide file tree
Showing 24 changed files with 2,683 additions and 534 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ http_archive(
# LICENSE: Common Public License 1.0
jvm_maven_import_external(
name = "junit",
artifact = "junit:junit:4.12",
artifact_sha256 = "59721f0805e223d84b90677887d9ff567dc534d7c502ca903c0c2b17f05c116a",
artifact = "junit:junit:4.13",
artifact_sha256 = "4b8532f63bdc0e0661507f947eb324a954d1dbac631ad19c8aa9a00feed1d863",
licenses = ["notice"], # Common Public License 1.0
server_urls = ["https://repo1.maven.org/maven2"],
)
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,15 +1,56 @@
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"],
)

ava_test(
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 = "greeting_test",
srcs = ["tests/com/example/GreetingTest.java"],
test_class = "com.example.GreetingTest",
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,
)
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));
}
}
2 changes: 1 addition & 1 deletion examples/java/greetings_project/hi/hi.bazelproject
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ additional_languages:
# dart
# kotlin
# python
# scala
# scala
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

0 comments on commit a5115d5

Please sign in to comment.