42
from Lambda to Alpha and Beyond … Sam Brannen @sam_brannen JUnit

JUnit 5 - from Lambda to Alpha and beyond

Embed Size (px)

Citation preview

Page 1: JUnit 5 - from Lambda to Alpha and beyond

from Lambda to Alpha and Beyond … Sam Brannen@sam_brannen

JUnit

Page 2: JUnit 5 - from Lambda to Alpha and beyond

2

Sam Brannen

• Spring and Java Consultant @

• Java Developer for over 17 years

• Spring Framework Core Committer since 2007

• Swiss Spring User Group Lead

• Trainer & Conference Speaker

• JUnit 5 Core Committer since October 2015

Page 3: JUnit 5 - from Lambda to Alpha and beyond

3

Swiftmind

Experts in Spring and Enterprise Java

Areas of expertise• Spring *• Java EE• Software Architecture• Software Development

Where you find us• Zurich, Switzerland• @swiftmind• http://www.swiftmind.com

Page 4: JUnit 5 - from Lambda to Alpha and beyond

4

A Show of Hands…

Page 5: JUnit 5 - from Lambda to Alpha and beyond

5

Agenda

• Impetus for Change

• JUnit Lambda

• Roadmap

• JUnit 5

• Feedback

• Q&A

Page 6: JUnit 5 - from Lambda to Alpha and beyond

6

Impetus for Change

Page 7: JUnit 5 - from Lambda to Alpha and beyond

7

Why a New Version of JUnit?

• JUnit 4.0 was released a decade ago– a lot has changed since then…– testing needs have matured– expectations have grown

• Modularity big ball of mud

• Test discovery and execution tightly coupled

• Extensibility lot of room for improvement

• Let’s not forget Java 8

Page 8: JUnit 5 - from Lambda to Alpha and beyond

8

Modularity in JUnit 4

• Sure there are packages– but… there’s only THE junit.jar

Page 9: JUnit 5 - from Lambda to Alpha and beyond

9

JUnit 4 Runner API

• Very powerful

• In fact, it can do anything

• But… you can’t combine Runners

• Parameterized + SpringJUnit4ClassRunner no way

Page 10: JUnit 5 - from Lambda to Alpha and beyond

10

JUnit 4… Rules… are meant to be broken

• JUnit 4.7: MethodRule (@Rule)• JUnit 4.9: TestRule (@Rule / @ClassRule)

• Great for simple use cases• Can even be combined

• But… a single rule can’t be used for method-level and class-level callbacks

• Plus… zero support for instance-level callbacks

• Case in point: SpringClassRule / SpringMethodRule

Page 11: JUnit 5 - from Lambda to Alpha and beyond

11

JUnit Lambda

Page 12: JUnit 5 - from Lambda to Alpha and beyond

12

Crowdfunding Campaign

• Initiated by Johannes Link and Marc Philipp

• Later joined by Matthias Merdes, Stefan Bechtold, & Sam Brannen

• Ran from July to October 2015

• Raised 53,937 Euros from 474 individuals and companies

• 4 companies donated 6 weeks of developer time

Page 13: JUnit 5 - from Lambda to Alpha and beyond

13

Thanks!

Page 14: JUnit 5 - from Lambda to Alpha and beyond

14

The Kick-off Team

Page 15: JUnit 5 - from Lambda to Alpha and beyond

15

Roadmap

• Prototype December 2nd, 2015

• 5.0.0-ALPHA February 1st, 2016

• 5.0.0-M1 in progress– tentative release end of June 2016

• M2, M3, … Summer 2016

• RC1, RC2, ... Fall 2016

• GA late 2016

Page 16: JUnit 5 - from Lambda to Alpha and beyond

16

JUnit 5

Page 17: JUnit 5 - from Lambda to Alpha and beyond

17

JUnit 5… in a Nutshell

• Modular

• Extensible

• Modern

• Forward and backward compatible– JUnit 5 supports JUnit 3.8 and JUnit 4– Can be run with JUnit 4

• @RunWith(JUnit5.class)

Page 18: JUnit 5 - from Lambda to Alpha and beyond

18

Architecture

• Test code depends only on the JUnit 5 API.

• IDEs and build tools depend on the Launcher and Engine APIs and can execute tests independent of the testing framework in use.

Page 19: JUnit 5 - from Lambda to Alpha and beyond

19

Modules

• junit5-api• junit-launcher• junit-engine-api• junit5-engine• junit4-engine• junit4-runner• junit-commons• junit-console• junit-gradle• surefire-junit5

Page 20: JUnit 5 - from Lambda to Alpha and beyond

20

Launcher API

• Used by IDEs and build tools to launch the framework

• Central API for discovering and executing tests via one or more engines

• TestDiscoveryRequest– selectors and filters

• Feedback provided via the TestExecutionListener API

Page 21: JUnit 5 - from Lambda to Alpha and beyond

21

TestEngine API

• Test engine discovers and executes tests– for a particular programming model

• Automatic discovery via Java’s ServiceLoader mechanism

• JUnit5TestEngine

• JUnit4TestEngine

• Implement your own…

Page 22: JUnit 5 - from Lambda to Alpha and beyond

22

JUnit 5 Extension Model

org.junit.gen5.api.extensions @ExtendWith(...)

• BeforeAllCallback• BeforeEachCallback

• BeforeTestExecutionCallback• AfterTestExecutionCallback

• AfterEachCallback• AfterAllCallback

• ContainerExecutionCondition• TestExecutionCondition• TestInstancePostProcessor• ParameterResolver• TestExecutionExceptionHandler

Page 23: JUnit 5 - from Lambda to Alpha and beyond

23

JUnit 5 Programming Model

org.junit.gen5.api

• Annotations and meta-annotations• Assertions and Assumptions• Custom display names• Visibility• Tagging• Conditional test execution• Dependency injection for constructors and methods• Lambda expressions and method references• Interface default methods• Nested test classes

Page 24: JUnit 5 - from Lambda to Alpha and beyond

24

Annotations

• @Test• @BeforeAll / @AfterAll• @BeforeEach / @AfterEach

• @DisplayName

• @Tag / @Tags

• @Disabled

• @Nested

Page 25: JUnit 5 - from Lambda to Alpha and beyond

25

Assertions

org.junit.gen5.api.Assertions

• Limited set of core assertions– assertEquals(), assertNotNull(), etc.– plus assertThrows() and expectThrows()– and assertAll()

• Supplier<String> for lazy failure message evaluation– message is now the last parameter

• For more power, use AssertJ, Hamcrest, etc.

Page 26: JUnit 5 - from Lambda to Alpha and beyond

26

Assumptions

org.junit.gen5.api.Assumptions

• Limited set of core assumptions– For aborting tests mid-flight

• assumeTrue() / assumeFalse()– BooleanSupplier, Supplier<String>

• assumingThat( ? , () -> {} );

Page 27: JUnit 5 - from Lambda to Alpha and beyond

27

DEMO

Page 28: JUnit 5 - from Lambda to Alpha and beyond

28

Test Names

• Names default to test class or test method names– characters limited based on Java syntax

• Custom display names @DisplayName– Can contain spaces, special chars, and even emoji 😱

Page 29: JUnit 5 - from Lambda to Alpha and beyond

29

Dependency Injection

• Extension Model meets Programming Model

• ParameterResolver extension– resolves parameters for constructors or methods

• TestInfo: inject into constructor, @Test, @BeforeEach, etc.– access display name, tags, class, method

• TestInfoParameterResolver– eating our own dog food ;-)

• See also:– TestReporter– MockitoExtension– SpringExtension

Page 30: JUnit 5 - from Lambda to Alpha and beyond

30

DEMO

Page 31: JUnit 5 - from Lambda to Alpha and beyond

31

Tagging

• Declare @Tag or @Tags on an interface, class, or method

@Tag("fast")@Testvoid myFastTest() {}

Page 32: JUnit 5 - from Lambda to Alpha and beyond

32

Custom Tags

• Declare @Tag or @Tags as a meta-annotation

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Tag("fast")@Testpublic @interface FastTest {}

@FastTestvoid myFastTest() {}

Page 33: JUnit 5 - from Lambda to Alpha and beyond

33

Conditional Test Execution

• Extension Model meets Programming Model

• ContainerExecutionCondition• TestExecutionCondition

• @Disabled• DisabledCondition

– eating our own dog food ;-)

• Deactivate via Launcher/System property– junit.conditions.deactivate = org.junit.*

Page 34: JUnit 5 - from Lambda to Alpha and beyond

34

DEMO

Page 35: JUnit 5 - from Lambda to Alpha and beyond

35

Interface Default Methods

• Introduces the concept of a test interface– Enables multiple inheritance in tests– Kinda like testing traits

• @BeforeEach / @AfterEach• @Test• @Tag• @ExtendWith

• See StringTests example in user guide

Page 36: JUnit 5 - from Lambda to Alpha and beyond

36

Nested Test Classes

• Enables logical, hierarchical grouping of test classes– with shared initialization and state from outer classes

• Declare @Nested on non-static nested classes– i.e., inner classes

• You can even combine nested classes and test interfaces

• See TestingAStack example in user guide

Page 37: JUnit 5 - from Lambda to Alpha and beyond

37

Spring Support for JUnit 5

• SpringExtension– @ExtendWith(SpringExtension.class)– https://github.com/sbrannen/spring-test-junit5

• Works with Spring Framework 4.3

• Already supports:– Core Spring TestContext Framework features– Constructor and method injection via @Autowired,

@Qualifier, @Value

• Fully integrated in Spring Framework 5.0

Page 38: JUnit 5 - from Lambda to Alpha and beyond

38

DEMO

Page 39: JUnit 5 - from Lambda to Alpha and beyond

39

In Closing…

Page 40: JUnit 5 - from Lambda to Alpha and beyond

40

What’s Missing?

• Official IDE and build integration

• Dynamic tests (M1)– registered as lambdas, streams, collections, etc.

• Parameterized tests (M2)

• Scenario tests (M3)

• Parallel execution (?)

• …

Page 41: JUnit 5 - from Lambda to Alpha and beyond

41

Resources and Feedback Channels• Project Homepage

– http://junit.org/junit5

• User Guide– http://junit.org/junit5/docs/current/user-guide

• Javadoc– https://junit.ci.cloudbees.com/job/JUnit5/javadoc

• GitHub– https://github.com/junit-team/junit5

• Sample Projects– https://github.com/junit-team/junit5-samples

• Twitter– https://twitter.com/JUnitTeam

• Stack Overflow– http://stackoverflow.com/tags/junit5

Page 42: JUnit 5 - from Lambda to Alpha and beyond

42

Q & A

Sam Brannen

@sam_brannenwww.slideshare.net/sbrannenwww.swiftmind.com