Skip to content

Commit a05b6b6

Browse files
authored
Merge pull request eugenp#5429 from johanjanssen/master
BAEL-2263 Using JUnit 5 with Gradle
2 parents b2bea50 + 01819fe commit a05b6b6

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎gradle/junit5/build.gradle‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
// Apply the java-library plugin to add support for Java Library
3+
id 'java-library'
4+
}
5+
6+
dependencies {
7+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
8+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
9+
10+
// Only necessary for JUnit 3 and 4 tests
11+
testCompileOnly 'junit:junit:4.12'
12+
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
13+
14+
}
15+
16+
repositories {
17+
jcenter()
18+
}
19+
20+
test {
21+
useJUnitPlatform {
22+
includeTags 'fast'
23+
excludeTags 'slow'
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CalculatorJUnit4Test {
8+
@Test
9+
public void testAdd() {
10+
assertEquals(42, Integer.sum(19, 23));
11+
}
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Tag;
7+
import org.junit.jupiter.api.Test;;
8+
9+
public class CalculatorJUnit5Test {
10+
11+
@Tag("fast")
12+
@Test
13+
public void testAdd() {
14+
assertEquals(42, Integer.sum(19, 23));
15+
}
16+
17+
@Tag("slow")
18+
@Test
19+
public void testAddMaxInteger() {
20+
assertEquals(2147483646, Integer.sum(2147183646, 300000));
21+
}
22+
23+
@Tag("fast")
24+
@Test
25+
public void testAddZero() {
26+
assertEquals(21, Integer.sum(21, 0));
27+
}
28+
29+
@Tag("fast")
30+
@Test
31+
public void testDivide() {
32+
assertThrows(ArithmeticException.class, () -> {
33+
Integer.divideUnsigned(42, 0);
34+
});
35+
}
36+
}

‎gradle/settings.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ include 'greeting-library'
55
include 'greeting-library-java'
66
include 'greeter'
77
include 'gradletaskdemo'
8-
8+
include 'junit5'
99

1010
println 'This will be executed during the initialization phase.'

0 commit comments

Comments
 (0)