46
@g_o_rge JUnit - State of the Union © Görge Albrecht 2016 JUnit State of the Union 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

  • Upload
    others

  • View
    34

  • Download
    1

Embed Size (px)

Citation preview

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

@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

Görge Albrecht

Software Developer since1989

Freelance Code Mentor

"taking care of code"

Need help writing simpler code? Contact me!

@g_o_rge

http://code­mentor.com

goerge@code­mentor.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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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://code­mentor.com

Software Quality Days, Vienna

1