-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJUnitJupiterTest.java
More file actions
67 lines (54 loc) · 2.09 KB
/
JUnitJupiterTest.java
File metadata and controls
67 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.github.timtebeek.betterassertions.junit5;
import com.github.timtebeek.betterassertions.Book;
import com.github.timtebeek.betterassertions.Bundle;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class JUnitJupiterTest {
// JUnit Jupiter
// + Reduced visibility of classes and methods
// + Improved assertion messages
@Test
void bundle() {
List<Book> books = new Bundle().getBooks();
assertNotNull(books);
assertEquals(3, books.size());
assertTrue(books.contains(new Book("Effective Java", "Joshua Bloch", 2001)));
assertTrue(books.contains(new Book("Java Concurrency in Practice", "Brian Goetz", 2006)));
assertTrue(books.contains(new Book("Clean Code", "Robert C. Martin", 2008)));
assertFalse(books.contains(new Book("Java 8 in Action", "Raoul-Gabriel Urma", 2014)));
Book book = books.get(0);
assertEquals("Effective Java", book.getTitle(), "Title should match");
}
// Test passes, but the assertions are incorrect!
@Test
void incorrectArgumentOrder() {
String expectedTitle = new Bundle().getBooks().get(0).getTitle();
assertNotNull("Title not null", expectedTitle);
assertEquals(expectedTitle, "Effective Java", "Title should match");
}
// Exception thrown by the test
@Test
void expectException() {
Exception ex = assertThrows(IllegalArgumentException.class, () -> {
int i = 1 + 2;
assertEquals(i, 3);
boom();
});
assertEquals("boom!", ex.getMessage());
}
private void boom() {
throw new IllegalArgumentException("boom!");
}
// Missing @Nested annotation
class InnerClass {
@Test
void innerClass() {
assertTrue(true);
}
}
}