![Page 1: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/1.jpg)
@g_o_rge JUnit - State of the Union © Görge Albrecht2016
JUnit
State of the Union
1
![Page 2: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/2.jpg)
Görge Albrecht
Software Developer since1989
Freelance Code Mentor
"taking care of code"
Need help writing simpler code? Contact me!
@g_o_rge
http://codementor.com
goerge@codementor.com
1
![Page 3: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/3.jpg)
History of JUnit
1
![Page 4: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/4.jpg)
The Beginnings
SUnit written by Kent Beck around 1992 due to the lack of automatic testingpossibilities
JUnit created by Kent Beck and Erich Gamma around 1997
JUnit became de facto standard of testing Java code
— Martin Fowler
Never in the field of software development was somuch owed by so many to so few lines of code.
1
![Page 5: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/5.jpg)
JUnit 4
steady progress up to JUnit 3.8.x
David Saff joined Kent Beck
big architectural change from framework style (subclassing) to DSL Style(annotations)
added dependency to
added Rules to setting the context of a test
JUnit 4.4 Hamcrest
JUnit 4.7
1
![Page 6: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/6.jpg)
JUnit ƛ
10 Years since release of Junit 4
Java 8 introduced Lambdas
not supported nor utilized by JUnit
JUnit has always been a pet project
to fund next generation of JUnitCrowdfunding Campaign on Indiegogo
1
![Page 7: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/7.jpg)
JUnit 5
development started in Autumn 2015
alpha release 2016-02-01
M1 released on 2016-07-07
M2 released on 2016-07-23
JUnit 5.0 GA expected by end of 2016
1
![Page 8: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/8.jpg)
Decouple test execution and reporting from test definition and provisioning
Rethinking JUnit’s extensibility story
Making use of Java 8 features
Vision
1
![Page 9: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/9.jpg)
Lessons learned from JUnit 4"If you release it, you’re going to maintain it."
Parameterized
Hamcrest
1
![Page 10: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/10.jpg)
Prefer extension points over features
an extension point should be good at one thing
It should be hard to write tests that behave differently based on how they arerun
Tests should be easy to understand
Minimize dependencies (especially third-party)
JUnit 5 Core Principles
1
![Page 11: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/11.jpg)
Components
JUnit Platform
foundation for launching test frameworks
JUnit Jupiter
new programming model and extension model
JUnit Vintage
TestEngine for running JUnit 3 and JUnit 4 based tests on the platform
Open Test Alliance for the JVM: opentest4j
1
![Page 12: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/12.jpg)
Syntactic sugar
1
![Page 13: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/13.jpg)
no public anymore
package com.codementor.junit5;
import org.junit.jupiter.api.Test;
class NoPublicTest {
@Test void name() { }}
1
![Page 14: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/14.jpg)
Display Names
@DisplayName("Custom names can be set on class …")class DisplayNamesTest {
@Test @DisplayName("… and method level") void name() { }}
1
![Page 15: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/15.jpg)
Contract Tests I
class ContractTest implements EqualsTester<String> {
@Override public String value() { return "one"; }
@Override public String equalValue() { return "one"; }
@Override public String nonEqualValue() { return "two"; }}
1
![Page 16: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/16.jpg)
Contract Tests II
interface EqualsTester<T> {
T value();
T equalValue();
T nonEqualValue();
@Test default void twoValuesMustBeEqual() { assertEquals(value(), equalValue()); }
@Test default void nonEqualValuesMustNotMatch() { assertNotEquals(value(), nonEqualValue()); }
1
![Page 17: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/17.jpg)
Contract Tests III
@Test default void valueMustNotBeEqualToNull() { assertNotEquals(value(), null); }
@Test default void sameValueMustbeEqual() { assertEquals(value(), value()); }}
1
![Page 18: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/18.jpg)
Contract Tests IV
1
![Page 19: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/19.jpg)
Dynamic Test Creation
IntelliJ support not complete yet!
@TestFactory Stream<DynamicTest> dynamicTestCreation() { return IntStream.iterate(2, n -> n * 2).limit(4).mapToObj( n -> dynamicTest("test " + n, () -> assertTrue(n % 2 == 0)) ); }
@Test void convinceIntelliJThisIsATest() { }
1
![Page 20: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/20.jpg)
Nested Test
@DisplayName("A new int…")class NestedClassesTest {
private int count = MIN_VALUE;
@BeforeEach void setCountToZero() { count = 0; }
@Test @DisplayName("… is almost nothing…") void countIsZero() { assertEquals(0, count); }
@Nested @DisplayName("… but after a step down…") class CountGreaterZeroTest {
@BeforeEach void increaseCount() { count++; }
1
![Page 21: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/21.jpg)
Nested Test Output
1
![Page 22: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/22.jpg)
Assertions
1
![Page 23: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/23.jpg)
Message comes last, finally!
@Test void messageComesLast() { org.junit.Assert.assertEquals( "JUnit <=4 message comes first", "aString", "aString");
org.junit.jupiter.api.Assertions.assertEquals( "aString", "aString", "JUnit 5 message comes last"); }
1
![Page 24: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/24.jpg)
Lambdas FTW
@Test void assertWithBooleanClassic() { assertTrue(true); }
@Test void assertWithBooleanLambda() { assertTrue(() -> true); }
@Test void assertWithBooleanMethodReference() { assertTrue(TRUE::booleanValue); }
1
![Page 25: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/25.jpg)
Exceptions
@Test void exception() { expectThrows(RuntimeException.class, this::illegal); }
private void illegal() { throw new IllegalArgumentException("illegal"); }
1
![Page 26: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/26.jpg)
Exception Message Assertion
@Test void exceptionMessageAsssertion() { final RuntimeException exception = expectThrows(RuntimeException.class, this::illegal); assertEquals("illegal", exception.getMessage()); }
private void illegal() { throw new IllegalArgumentException("illegal"); }
1
![Page 27: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/27.jpg)
Lazy message evaluation
@Test void assertWithLambdaMessage() { assertTrue( true, () -> "JUnit 5" + " message are evaluated " + "lazily"); }
1
![Page 28: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/28.jpg)
Assert all properties at once
@Test void assertAllProperties() { Address address = new Address("New City", "Some Street", "No");
assertAll( () -> assertEquals("New City", address.city), () -> assertEquals("Irgendeinestraße", address.street), () -> assertEquals("Nr", address.number) ); }
1
![Page 29: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/29.jpg)
Assumptions
1
![Page 30: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/30.jpg)
Volkswagening
@Test void volkswageningDoesNotWorkWithAssumeAsThisIsReportedAsAborted() { assumeTrue(notOnTestStation()); assertTrue(abgasWerteUnterVorgabe()); }
@Test void assumingThatIsTheKeyToVolkswagening() { assumingThat(notOnTestStation(), () -> assertTrue(abgasWerteUnterVorgabe())); }
1
![Page 31: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/31.jpg)
Extensions & Dependency Injection
1
![Page 32: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/32.jpg)
Extensions
no Runner and @Rule / @ClassRule anymore
replaced by Extensions
registered via @ExtendWith on class, method or annotation
multiple extensions per element possible
registered extensions are inherited
1
![Page 33: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/33.jpg)
Extensions example
@ExtendWith(MockitoExtension.class)@ExtendWith(VertxExtension.class)class MyTest {
@Test void test1() {}
@ExtendWith(TimingExtension.class) @Test void test2() {}}
1
![Page 34: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/34.jpg)
Extension Types I
Conditional Test Execution
ContainerExecutionCondition
TestExecutionCondition
Test Instance Post-processing
TestInstancePostProcessor (MockitoExtension,SpringExtension)
Parameter Resolution
ParameterResolver
1
![Page 35: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/35.jpg)
Extension Types II
Test Lifecycle Callbacks
BeforeAllCallback
BeforeEachCallback
BeforeTestExecutionCallback
AfterTestExecutionCallback
AfterEachCallback
AfterAllCallback
Exception Handling
TestExecutionExceptionHandler
1
![Page 36: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/36.jpg)
JUnit 4.x: DI via Runner setting method parameters
only one Runner per test class possible
JUnit 4.7: DI via @Rule settings fields in test class
multiple fields if test methods had different dependencies
JUnit 5: ParameterResolver FTW
multiple ParameterResolver per test method possible
each ParameterResolver can handle different parameter types
Dependency Injection
1
![Page 37: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/37.jpg)
DI example
class MyTest {
@ExtendWith(MockitoExtension.class) @ExtendWith(VertxExtension.class) @Test void test(@Mock MyService service, TestContext context) {}}
1
![Page 38: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/38.jpg)
Built-in ParameterResolver
TestInfoParameterResolver
provides test with context (name, tags, etc.) via TestInfo parameter
works also on lifecycle callbacks
TestReporterParameterResolver
injects TestReporter into test
test can provide runtime information via TestReporter to execution env
1
![Page 40: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/40.jpg)
Remarks
JUnit 5 is a complete rewrite
neither compile nor binary compatibility
bridges provided down- and upwards for smooth migration
⇒ no need to update all existings tests
1
![Page 41: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/41.jpg)
Execution
JUnit 4 based Runner JUnitPlatform to run JUnit 5 tests in a JUnit 4 env
JUnit Platform engine junit-vintage-engine to run JUnit 3 and 4 tests ina JUnit 5 env
1
![Page 42: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/42.jpg)
Packages
JUnit < 4 junit
JUnit 4.x org.junit
JUnit 5 org.junit.jupiter.api
1
![Page 43: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/43.jpg)
Assertions & Assumptions
JUnit 5 JUnit 4.x
JUnit 5 JUnit 4.x
o.j.j.a.Assertions o.j.Assert
o.j.j.a.Assumptions o.j.Assume
1
![Page 44: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/44.jpg)
Annotations
JUnit 5 JUnit 4.x
JUnit 5 JUnit 4.x
@Test (new package) @Test
@TestFactory -
@Disabled @Ignore
@DisplayName -
@Tag @Category
1
![Page 45: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/45.jpg)
Lifecycle Annotations
JUnit 5 JUnit 4.x
JUnit 5 JUnit 4.x
@BeforeEach @Before
@AfterEach @After
@BeforeAll @BeforeClass
@AfterAll @AfterClass
@ExtendWith ~ @RunWith, @Rule, @ClassRule
1
![Page 46: JUnit - code-mentor.com€¦ · JUnit created by Kent Beck and Erich Gamma around 1997 JUnit became de facto standard of testing Java code — Martin Fowler Never in the field of](https://reader033.vdocument.in/reader033/viewer/2022052320/604d5cd44adaa16e76109e9c/html5/thumbnails/46.jpg)
Thanks for listening@g_o_rge ∙
Erste Schritte mit JUnit 5
Workshop with Peter CodeCop Kofler, 17.01.2017
Lesbare Tests mit AssertJ
Talk, 19.01.2017
http://codementor.com
Software Quality Days, Vienna
1