Skip to content

Commit 5fc64d1

Browse files
authored
add JUnit5 code (thombergs#217)
* add JUnit5 code * modify JUnit5 code
1 parent 179201f commit 5fc64d1

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed

junit/junit5/junit5/pom.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>junit5</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter-api</artifactId>
20+
<version>5.9.0</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-params</artifactId>
27+
<version>5.9.0</version>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.hamcrest</groupId>
33+
<artifactId>hamcrest-all</artifactId>
34+
<version>1.3</version>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
</project>
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Cat {
2+
private String name;
3+
4+
public Cat(String name) {
5+
this.name = name;
6+
}
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public Cat() {
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
public class GoldFish {
3+
private String name;
4+
private int age;
5+
6+
public GoldFish(String name, int age) {
7+
this.name = name;
8+
this.age = age;
9+
}
10+
public int calculateSpeed() {
11+
if (age == 0){
12+
throw new RuntimeException("This will fail :((");
13+
}
14+
return 10 / age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public int getAge() {
22+
return age;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
import java.util.List;
4+
5+
import static java.util.Arrays.asList;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.*;
8+
9+
public class CatTest {
10+
@Test
11+
public void testMeow() {
12+
String catName = "Stilla";
13+
int catAge = 3;
14+
boolean isNice = false;
15+
16+
assertThat(catName, equalTo("Stilla"));
17+
assertThat(catAge, lessThan(5));
18+
assertThat(isNice, is(false));
19+
}
20+
21+
@Test
22+
public void testCatInstance() {
23+
Cat cat = new Cat();
24+
25+
assertThat(cat, instanceOf(Cat.class));
26+
}
27+
28+
@Test
29+
public void testSameCatInstance() {
30+
Cat cat = new Cat();
31+
32+
assertThat(cat, sameInstance(cat));
33+
}
34+
35+
@Test
36+
public void testCollectionContaining() {
37+
List<String> catNames = asList("Phibi", "Monica", "Stilla");
38+
39+
assertThat(catNames, hasItems("Monica", "Phibi"));
40+
assertThat(catNames, not(hasItems("Melih")));
41+
}
42+
43+
@Test
44+
public void testCollectionSize() {
45+
List<String> catNames = asList("Phibi", "Monica");
46+
47+
assertThat(catNames, hasSize(2));
48+
}
49+
50+
@Test
51+
public void testBean() {
52+
Cat cat = new Cat("Mimi");
53+
54+
assertThat(cat, hasProperty("name", equalTo("Mimi")));
55+
}
56+
57+
@Test
58+
public void testStringEquality() {
59+
String catNameInCaps = "RACHEL";
60+
61+
assertThat(catNameInCaps, equalToIgnoringCase("rachel"));
62+
}
63+
64+
@Test
65+
public void testStringContains() {
66+
String catName = "Joey The Cute";
67+
68+
assertThat(catName, containsString("Cute"));
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import org.junit.jupiter.api.*;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
public class DogTest {
6+
7+
@BeforeAll
8+
public static void init() {
9+
System.out.println("Doing stuff");
10+
}
11+
12+
@BeforeEach
13+
public void doEach() {
14+
System.out.println("Hey Doggo");
15+
}
16+
17+
@AfterAll
18+
public static void finish() {
19+
System.out.println("Finishing stuff");
20+
}
21+
22+
@AfterEach
23+
public void doAfterEach() {
24+
System.out.println("Bye Doggo");
25+
}
26+
27+
@Test
28+
public void barkFailure() {
29+
String expectedString = "Meow";
30+
assertEquals(expectedString, "Woof");
31+
}
32+
33+
@Disabled("Dog 1 please don't woof")
34+
@Test
35+
public void testBark1() {
36+
String expectedString = "woof1";
37+
assertEquals(expectedString, "woof1");
38+
System.out.println("WOOF => 1");
39+
}
40+
41+
@Test
42+
public void testBark2() {
43+
String expectedString = "woof2";
44+
assertEquals(expectedString, "woof2");
45+
System.out.println("WOOF => 2");
46+
}
47+
48+
@Test
49+
public void testNotBark() {
50+
String unexpectedString = "";
51+
assertNotEquals(unexpectedString, "woof");
52+
System.out.println("Didn't woof!!");
53+
}
54+
55+
@Test
56+
public void nullCheck() {
57+
Object dog = null;
58+
assertNull(dog);
59+
System.out.println("Null dog :(");
60+
}
61+
62+
@Test
63+
public void nonNullCheck() {
64+
String dog = "Max";
65+
assertNotNull(dog);
66+
System.out.println("Hey I am " + dog);
67+
}
68+
69+
@Test
70+
public void trueCheck() {
71+
int dogAge = 2;
72+
assertTrue(dogAge < 5);
73+
System.out.println("I am young :)");
74+
}
75+
76+
@Test
77+
public void falseCheck() {
78+
int dogAge = 7;
79+
assertFalse(dogAge < 5);
80+
System.out.println("I am old :(");
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import org.junit.jupiter.api.Test;
2+
import org.junit.jupiter.params.ParameterizedTest;
3+
import org.junit.jupiter.params.provider.Arguments;
4+
import org.junit.jupiter.params.provider.MethodSource;
5+
import java.util.stream.Stream;
6+
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.equalToIgnoringCase;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
12+
13+
public class GoldFishTest {
14+
@Test
15+
public void testBooleanAssumption() {
16+
GoldFish goldFish = new GoldFish("Windows Jelly", 1);
17+
18+
assumeTrue(System.getProperty("os.name").contains("Windows"));
19+
assertThat(goldFish.getName(), equalToIgnoringCase("Windows Jelly"));
20+
}
21+
22+
@Test
23+
public void testBooleanAssert() {
24+
GoldFish goldFish = new GoldFish("Windows Jelly", 1);
25+
26+
assert(System.getProperty("os.name").contains("Windows"));
27+
assertThat(goldFish.getName(), equalToIgnoringCase("Windows Jelly"));
28+
}
29+
30+
@Test
31+
public void testException() {
32+
GoldFish goldFish = new GoldFish("Goldy", 0);
33+
34+
RuntimeException exception = assertThrows(RuntimeException.class, goldFish::calculateSpeed);
35+
36+
assertThat(exception.getMessage(), equalToIgnoringCase("This will fail :(("));
37+
}
38+
39+
@ParameterizedTest
40+
@MethodSource("provideFishes")
41+
public void parameterizedTest(GoldFish goldFish) {
42+
assertTrue(goldFish.getAge() >= 1);
43+
}
44+
45+
private static Stream<Arguments> provideFishes() {
46+
return Stream.of(
47+
Arguments.of(new GoldFish("Browny", 1)),
48+
Arguments.of(new GoldFish("Greeny", 2))
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)