Skip to content

Commit 3585745

Browse files
committed
Update Code, Update README therefore
1 parent 59c170b commit 3585745

14 files changed

+299
-292
lines changed

README.adoc

Lines changed: 208 additions & 191 deletions
Large diffs are not rendered by default.

src/test/java/com/drandarov/bestPractice/JUnit_BestPractice.java

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.drandarov.bestPractice.utils.DummyUtil;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.BeforeEach;
7-
import org.junit.jupiter.api.Disabled;
87
import org.junit.jupiter.api.Test;
8+
import org.opentest4j.AssertionFailedError;
99

1010
import java.util.HashMap;
1111
import java.util.Map;
@@ -16,9 +16,6 @@
1616
/**
1717
* A demo-class with a collection of simple best-practices when writing JUnit-Tests.
1818
*
19-
* I {@link Disabled} the failing demo-tests, since I don't like failing tests in CI-Environments, however you can still
20-
* run them singularly in your IDE. ;)
21-
*
2219
* @author dmitrij-drandarov
2320
* @since 10 Feb 2017
2421
*/
@@ -39,72 +36,62 @@ void dummy() {
3936
##############################################*/
4037

4138
/**
42-
* Don't check for nulls with {@link Assertions#assertTrue}. Error messages will be meaningless and therefore
43-
* useless.
39+
* Don't check for nulls with {@link Assertions#assertTrue}. Error messages will be meaningless and therefore useless.
4440
* Do use {@link Assertions#assertNotNull} or {@link Assertions#assertNull} instead.
4541
*/
4642
@Test
47-
@Disabled
4843
void assertTrueNotNullTest() {
4944
// Unclear, useless error-message when tests fail
50-
assertTrue(dummyFruits.get(4) != null);
45+
assertThrows(AssertionFailedError.class, () ->
46+
assertTrue(dummyFruits.get(4) != null)).printStackTrace();
5147

52-
/*
53-
org.opentest4j.AssertionFailedError: // It couldn't be more useless...
54-
*/
48+
// org.opentest4j.AssertionFailedError // It couldn't be more useless...
5549
}
5650

5751
@Test
58-
@Disabled
5952
void assertNotNullTest() {
6053
// Better error-message you can actually use and understand
61-
assertNotNull(dummyFruits.get(4));
54+
assertThrows(AssertionFailedError.class, () ->
55+
assertNotNull(dummyFruits.get(4))).printStackTrace();
6256

63-
/*
64-
org.opentest4j.AssertionFailedError: expected: not <null>
65-
*/
57+
// org.opentest4j.AssertionFailedError: expected: not <null>
6658
}
6759

6860
/**
69-
* The same applies for using {@link #equals}. Use {@link Assertions#assertEquals} instead.
61+
* The same applies for using {@link Assertions#assertTrue} and {@link Object#equals}. Use {@link Assertions#assertEquals} instead.
7062
*/
7163
@Test
72-
@Disabled
7364
void assertTrueEqualsTest() {
7465
// Unclear, useless error-message when tests fail
75-
assertTrue(TYPE.BANANA.equals(dummyFruits.get(2).getType()));
66+
assertThrows(AssertionFailedError.class, () ->
67+
assertTrue(TYPE.BANANA.equals(dummyFruits.get(2).getType()))).printStackTrace();
7668

77-
/*
78-
org.opentest4j.AssertionFailedError: // It - again - couldn't be more useless...
79-
*/
69+
// org.opentest4j.AssertionFailedError: // It - again - couldn't be more useless...
8070
}
8171

8272
@Test
83-
@Disabled
8473
void assertEqualsTest() {
8574
// Really useful error-message you can actually use and understand
86-
assertEquals(TYPE.BANANA, dummyFruits.get(2).getType());
75+
assertThrows(AssertionFailedError.class, () ->
76+
assertEquals(TYPE.BANANA, dummyFruits.get(2).getType())).printStackTrace();
8777

88-
/*
89-
org.opentest4j.AssertionFailedError: expected: <BANANA> but was: <APPLE>
90-
Expected :BANANA
91-
Actual :APPLE
92-
*/
78+
// org.opentest4j.AssertionFailedError: expected: <BANANA> but was: <APPLE>
9379
}
9480

9581
/*##############################################
9682
# Working with delta
9783
##############################################*/
9884

9985
/**
100-
* It may seem trivial, but I've seen all of these in actual enterprise projects.
86+
* It may seem trivial, but they happen more often than you would assume.
10187
*/
10288
@Test
10389
void wrongDelta() {
10490
assertNotEquals(0.9, DummyUtil.calculateTimesThree(0.3)); // --> That's why the delta is important
10591

106-
/*Using a 0.0 delta doesn't make much sense and JUnit 5 actively prevents it (therefore the comment)
107-
assertEquals(20.9, DummyUtil.calculateTimesThree(19), 0.0); */
92+
// Using a 0.0 delta doesn't make much sense and JUnit 5 actively prevents it
93+
assertThrows(AssertionFailedError.class, () ->
94+
assertEquals(20.9, DummyUtil.calculateTimesThree(19), 0.0)).printStackTrace();
10895

10996
// With such a delta the correctness of the calculation is no longer assured
11097
assertEquals(15.5, DummyUtil.calculateTimesThree(5.0), 0.5);
@@ -137,31 +124,26 @@ void idealDelta() {
137124
##############################################*/
138125

139126
@Test
140-
@Disabled
141127
void wrongOrderTest() {
142-
assertEquals(dummyFruits.get(2).getName(), "Grapefruit"); //Gives you unclear, actually wrong fail-reports!
143-
// ^expected // ^actual
128+
//Gives you unclear, actually wrong fail-reports!
129+
assertThrows(AssertionFailedError.class, () ->
130+
assertEquals(dummyFruits.get(2).getName(), "Grapefruit")).printStackTrace();
131+
// . . . . . . . . . . ^expected . . . . . . . . . . // ^actual
144132

145-
/*
146-
org.opentest4j.AssertionFailedError: expected: <Granny Smith Apple> but was: <Baby Banana>
147-
Expected :Granny Smith Apple
148-
Actual :Baby Banana
149-
*/
133+
// org.opentest4j.AssertionFailedError: expected: <Granny Smith Apple> but was: <Grapefruit>
150134

151135
/* "So wait, the constant String I put in there is not an expected value? Tell me more about that!
152-
Or don't... Just fix the order and everything is fine ;)" */
136+
Or don't... Just fix the order and everything will be fine ;)" */
153137
}
154138

155139
@Test
156-
@Disabled
157140
void correctOrderTest() {
158-
assertEquals("Grapefruit", dummyFruits.get(2).getName()); // Clear and useful fail-report ;)
141+
// Clear and useful fail-report ;)
142+
assertThrows(AssertionFailedError.class, () ->
143+
assertEquals("Grapefruit", dummyFruits.get(2).getName())).printStackTrace();
144+
// . . . . . . . . . . ^expected . . . . . ^actual
159145

160-
/*
161-
org.opentest4j.AssertionFailedError: expected: <Grapefruit> but was: <Granny Smith Apple>
162-
Expected :Grapefruit
163-
Actual :Granny Smith Apple
164-
*/
146+
// org.opentest4j.AssertionFailedError: expected: <Grapefruit> but was: <Granny Smith Apple>
165147

166148
/* "Oh, my actual value is not what was expected? Well now I know what the problem is and I can fix it!" */
167149
}

src/test/java/com/drandarov/junit5/JUnit5_00_GeneralChanges.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.drandarov.junit5;
22

33
import org.junit.jupiter.api.*;
4+
import org.junit.jupiter.api.extension.ExecutionCondition;
45

56
import static org.junit.jupiter.api.Assertions.assertTrue;
67
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -18,7 +19,7 @@ class JUnit5_00_GeneralChanges {
1819
##############################################*/
1920

2021
/**
21-
* Tests are now only not allowed to be static / private. Latter also goes for @Before.../@After...
22+
* Tests now only must not be static or private. Latter also goes for @Before.../@After...
2223
* timeout = ? and expected = ? functionality has now moved elsewhere. See in {@link JUnit5_01_NewFeaturesBasics}
2324
*/
2425
@Test
@@ -45,14 +46,14 @@ void beforeEach() {}
4546

4647
/**
4748
* Annotation @Ignore was replaced by @{@link Disabled}. Sounds less negative.
48-
* However a reason for the deactivation will be printed.
49+
* However a reason for the deactivation will be printed which can be more advanced with features like {@link ExecutionCondition}.
4950
*/
50-
@Disabled
51+
//@Disabled
5152
@Test
5253
void disabledTest() {}
5354

5455
/**
55-
* JUnit 4's experimental @Category is now called {@link Tag}/{@link Tags}.
56+
* JUnit 4s experimental @Category is now called {@link Tag}/{@link Tags}.
5657
*/
5758
@Tag("abc")
5859
@Test

src/test/java/com/drandarov/junit5/JUnit5_01_NewFeaturesBasics.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ void assertThrowsTest() {
9696
}
9797

9898
/**
99-
* You can also use {@link Assertions#assertThrows(Class, Executable)} to get the {@link Exception}-Instance if you
100-
* need it.
99+
* You can also use {@link Assertions#assertThrows(Class, Executable)} to get the {@link Exception}-Instance if you need it.
101100
*/
102101
@Test
103102
void expectThrowsTest() {

src/test/java/com/drandarov/junit5/JUnit5_02_NewFeaturesAdvanced.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class JUnit5_02_NewFeaturesAdvanced {
3939
@ExtendWith({ClassName_ParameterResolver.class, ParameterIndex_ParameterResolver.class})
4040
void customParameterTest(String className, Long parameterIndex) {
4141
System.out.println(className); // Surrounding class name injected by ClassName_ParameterResolver
42-
System.out.println(parameterIndex.toString()); // Parameter-Index injected by ParameterIndex_ParameterResolver
42+
System.out.println(parameterIndex); // Parameter-Index injected by ParameterIndex_ParameterResolver
4343
}
4444

4545

src/test/java/com/drandarov/junit5/JUnit5_03_AdvancedTestSamples.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import org.junit.jupiter.api.Test;
1010
import org.junit.jupiter.api.extension.ExtendWith;
1111

12-
import java.util.ArrayList;
1312
import java.util.Calendar;
14-
import java.util.List;
1513
import java.util.stream.IntStream;
1614
import java.util.stream.LongStream;
1715

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
1818
/**
1919
* A class containing advanced test samples and utils of features introduced and explained before.
2020
*
@@ -48,8 +48,8 @@ void disabledOnWeekdaysTest() {}
4848
*/
4949
@UITest("../../sample.fxml")
5050
void userInterfaceTest(Pane root) {
51-
System.out.println(String.valueOf(root.getPrefWidth())); // 555.0 (defined in FXML-File)
52-
System.out.println(String.valueOf(root.getPrefHeight())); // 333.0 (defined in FXML-File)
51+
System.out.println(root.getPrefWidth()); // 555.0 (defined in FXML-File)
52+
System.out.println(root.getPrefHeight()); // 333.0 (defined in FXML-File)
5353
}
5454

5555
/**
@@ -62,12 +62,12 @@ void userInterfaceTest(Pane root) {
6262
@Test
6363
@Benchmarked
6464
void benchmarkedTest() {
65-
List<Integer> primes = new ArrayList<>();
6665
System.out.println("Calculating some primes...");
67-
IntStream.iterate(2, i -> i + 1)
68-
.filter(i -> LongStream.rangeClosed(2, (long)(Math.sqrt(i))).allMatch(n -> i % n != 0))
69-
.limit(55555)
70-
.forEach(primes::add);
66+
int primeCount = 200000;
67+
68+
assertEquals(primeCount, IntStream.iterate(2, i -> i + 1)
69+
.filter(i -> LongStream.rangeClosed(2, (long) (Math.sqrt(i))).allMatch(n -> i % n != 0))
70+
.limit(primeCount).toArray().length);
7171
}
7272

7373
}

src/test/java/com/drandarov/junit5/JUnit5_04_More_Advanced.java renamed to src/test/java/com/drandarov/junit5/JUnit5_04_MoreAdvanced.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
import org.junit.jupiter.api.Test;
44

55
/**
6-
* TODO.
76
*
87
* @author dmitrij-drandarov
98
* @since XX XXX 2017
109
*/
11-
class JUnit5_04_More_Advanced {
10+
class JUnit5_04_MoreAdvanced {
1211

1312
/*##############################################
1413
# xxxxxxxxxxxxxxxxxxxxxx
1514
##############################################*/
1615

1716
/**
18-
* TODO.
17+
*
1918
*/
2019
@Test
21-
void x() {
22-
}
20+
void x() {}
2321

2422
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.drandarov.junit5;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
/**
6+
*
7+
* @author dmitrij-drandarov
8+
* @since XX XXX 2017
9+
*/
10+
class JUnit5_05_AssertionFrameworks {
11+
12+
/*##############################################
13+
# xxxxxxxxxxxxxxxxxxxxxx
14+
##############################################*/
15+
16+
/**
17+
*
18+
*/
19+
@Test
20+
void x() {}
21+
22+
}

src/test/java/com/drandarov/junit5/JUnit5_05_Assertion_Frameworks.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/test/java/com/drandarov/junit5/utils/benchmarked/BenchmarkExtension.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import static java.lang.System.currentTimeMillis;
1111

1212
/**
13-
* Extension, that does the logging for the benchmarks. (Implementation is not accurate!)
13+
* Extension, that does the logging for the benchmarks. (Implementation is not accurate or performant!)
1414
*
1515
* @author dmitrij-drandarov
1616
* @since 29 Jul 2016
1717
*/
18-
public class BenchmarkExtension implements BeforeAllCallback, BeforeTestExecutionCallback,
19-
AfterTestExecutionCallback, AfterAllCallback {
18+
public class BenchmarkExtension implements BeforeAllCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, AfterAllCallback {
2019

2120
private static final String APD = "\t-\t";
2221

0 commit comments

Comments
 (0)