44import com .drandarov .bestPractice .utils .DummyUtil ;
55import org .junit .jupiter .api .Assertions ;
66import org .junit .jupiter .api .BeforeEach ;
7- import org .junit .jupiter .api .Disabled ;
87import org .junit .jupiter .api .Test ;
8+ import org .opentest4j .AssertionFailedError ;
99
1010import java .util .HashMap ;
1111import java .util .Map ;
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 }
0 commit comments