JUnit 5 - from Lambda to Alpha and beyond
-
Upload
sam-brannen -
Category
Software
-
view
1.829 -
download
0
Transcript of JUnit 5 - from Lambda to Alpha and beyond
![Page 1: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/1.jpg)
from Lambda to Alpha and Beyond … Sam Brannen@sam_brannen
JUnit
![Page 2: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/4.jpg)
4
A Show of Hands…
![Page 5: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/5.jpg)
5
Agenda
• Impetus for Change
• JUnit Lambda
• Roadmap
• JUnit 5
• Feedback
• Q&A
![Page 6: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/6.jpg)
6
Impetus for Change
![Page 7: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/11.jpg)
11
JUnit Lambda
![Page 12: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/13.jpg)
13
Thanks!
![Page 14: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/14.jpg)
14
The Kick-off Team
![Page 15: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/16.jpg)
16
JUnit 5
![Page 17: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/24.jpg)
24
Annotations
• @Test• @BeforeAll / @AfterAll• @BeforeEach / @AfterEach
• @DisplayName
• @Tag / @Tags
• @Disabled
• @Nested
![Page 25: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/27.jpg)
27
DEMO
![Page 28: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/30.jpg)
30
DEMO
![Page 31: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/34.jpg)
34
DEMO
![Page 35: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/38.jpg)
38
DEMO
![Page 39: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/39.jpg)
39
In Closing…
![Page 40: JUnit 5 - from Lambda to Alpha and beyond](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/40.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/41.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051706/58ed93491a28abbc628b4601/html5/thumbnails/42.jpg)
42
Q & A
Sam Brannen
@sam_brannenwww.slideshare.net/sbrannenwww.swiftmind.com