Upload
trannhan
View
230
Download
2
Embed Size (px)
Citation preview
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
JUnit 5
Steve MoyerSteve Seltzer
Niraja Ramesh
The Pennsylvania State University
Features, Architecture, and Extensibility
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
About Us
● Steve Moyer– Enterprise Architect
● Steve Seltzer– Programmer/Analyst
● Niraja Ramesh– Programmer/Analyst
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
About Us● Enterprise Infrastructure and Operations
● Core IT support organization for Penn State
● Software Engineering Unit
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Agenda
● Rationale● Architecture● Principles● Features● Code examples● Future directions ● References
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Rationale
● Decouple test execution and reporting from test definition and provisioning
● Rethinking the JUnit’s extensibility story● Support for Java 8 features (Lambdas, Streams,
Interface default methods) for better assertions, generating test cases, formulating test hierachies, testing asynchronous code and other stuff
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Architecture
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Architecture
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
What do I need?
● junit-jupiter-api – to compile test classes and/or extensions
● junit-jupiter-engine (and dependencies) – to run JUnit 5 tests
● junit-vintage-engine (and dependencies) – to run JUnit 4 tests (using JUnit 5)
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
What do I need?
● junit-platform-console (and dependencies) – to execute JUnit 5 tests from the command line
● junit-platform-runner (and dependencies) – to execute JUnit 5 tests using JUnit 4
– provides a way to run JUnit 5 tests in Eclipse and (some) other tools that don’t yet natively support JUnit 5.
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
What do I need?
● junit-platform-maven-provider – to execute tests using JUnit 5 during a Maven build.
– See the Maven example at: https://github.com/junit-team/junit5-samples/tree/master/junit5-maven-consumer
● junit-platform-gradle-plugin – to execute tests using JUnit 5 during a Gradle build.
– See the Gradle example at: https://github.com/junit-team/junit5-samples/tree/master/junit5-gradle-consumer
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Set upplugin
https://github.com/PennState/junit5-presentation/blob/master/pom.xml
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Set updependencies
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Principles
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Principles
● 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 are run● Tests should be easy to understand● Minimize dependencies (especially third-party)
https://github.com/junit-team/junit5/wiki/Core-Principles
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Features
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Features
● Meta-Annotations ● Tags and filters● Dependency injection through parameters
on constructors and methods● Interface default methods● Dynamic tests (runtime)● Extensions
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Features● API Isolation
– Jupiter API for writing tests and extensions
– Engine API for writing new engines
● API Evolution– API annotations
● Internal ● Deprecated● Experimental ● Maintained ● Stable
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Code Examples
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Code Examples● Assertion tests● Exception tests● Assumption tests● Meta-Annotation● Dynamic tests● Extension tests● Grouped tests● Nested tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assertions
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assertion TestsSimple Assertions
Junit 4 Junit 5
assert...(expected, actual) assert...(expected, actual)
assert...(message, expected, actual) assert...(expected, actual, message)
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assertion TestsSimple Assertions
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assertion Tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assertion Tests
AssertionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Maven Output
output - txt file
output - xml file
maven console output
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Available Assertions
http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assertions.html
● assertTrue
● assertFalse
● assertEquals
● assertNotEquals
● assertArrayEquals
● fail
● assertNull
● assertNotNull
● assertSame
● assertNotSame
● assertAll
● assertThrows
● expectThrows
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
ExceptionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
SampleStudents.java
The class builds data models for freshman, part-time, full-time and so forth.
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
BuildTestCourses.java
This class creates different courses that we will register the students throughout our tests.
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
CourseUtils.java
This class registers courses for the students.Throws exception if their total credits exceed MAX_ALLOWED.
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
Total credits for all courses exceed 25‘registerForCourse’ method throws an exception
assertThrows compares the thrown exception class to the expected one
ExceptionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Exception Tests
ExceptionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assumptions
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assumptions Tests
● assumeTrue● assumeFalse● assumingThat
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assumption TestsassumeTrue
AssumptionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assumptions Test
AssumptionTests.java
assumeFalse
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Assumptions TestassumingThat
AssumptionTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Meta-Annotations
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Meta-Annotation
UseridCaseTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Tagging & Filtering
TaggingTest.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Dynamic Tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Dynamic Test
● Test case generated at run-time by a factory method @TestFactory
● Composed of a display name and an Executable● Executable is a functional interface – meaning tests
can be provided as lambda expressions or method references
● Because of dynamic nature, lifecycle callbacks are not applicable
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Dynamic Testing
DynamicTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extensibility
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Points
● ParameterResolver● ContainerExecutionCondition● TestExecutionCondition● TestInstancePostProcessor
● BeforeAllCallback● AfterAllCallback● BeforeEachCallback● AfterEachCallback● BeforeTestExecutionCallback● AfterTestExecutionCallback
http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/extension/Extension.html
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension PointTestExecutionCondition
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Test
This class disables a test if the method name does not contain the word ‘affiliate’
AfflliateDetector.java
implements TestExecutionCondition
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Test
Class ExtensionDisabledTests contains a method that will return a boolean indicating whether method name contains the word ‘student’
Register AffiliateDetector extension via @ExtendWith
ExtensionDisabledTests.java
implements TestExecutionCondition
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
ExtensionDisabledTests.java
Contains “Affiliate” (passes TestExecutionCondition)Contains “Student” (passes studentDetector method )
Contains “Affiliate” (passes TestExecutionCondition)No “Student” (fails studentDetector method )
implements TestExecutionConditionExtension Test
No “Affiliate” (fails TestExecutionCondition)
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension PointParameterResolver
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Built-in ParameterResolvers
● TestInfo– If a method parameter of type TestInfo is
resolved, JUnit will supply an instance that allows a test to retrieve the display name, tags, test class and test method.
● TestReporter– If a method parameter of type
TestReporter is resolved, JUnit will supply an instance that allows a test to publish entries to the output report.
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Test
looks for @UseridTest annotation to return true/false
returns value for given parameters(in the example, a String and a boolean)
UseridParameterResolver.java
implements ParameterResolver
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Test
UseridCaseTests.java
implements ParameterResolver
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension PointCallbacks
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Test
ChildTests – extends ParentTests
ParentTests – registers extension with class implementing callbacks
class implementing callbacks
Callback Example
implements Callback
Each class prints out the methods available within that class
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Extension Testsimplements Callback
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Grouped Tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Grouped Tests
● Traditional For Loop● assertAll()● Factory Tests (@TestFactory)
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Collection Testing
Halts test execution when course prerequisite is found
LoopingTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
AssertAll
Executes all test cases inside the assertAll block
LoopingTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Factory Test
Executes all tests regardless of failures
LoopingTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Nested Tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Nested Tests
FakeSuiteTests.java
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Future Directions
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Future
● M3 (September 30, 2016)– JUnit 4 interoperability
– Additional discovery selectors
– Documentation
● M4 (October 30, 2016)– Parameterized tests
– Enhanced dynamic tests
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Future
● M5 (November 29, 2016)– Scenario tests
– Repeated tests
– Test execution in user-defined thread
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
Questions / Comments?
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
References
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
References● This presentation
– See the presentation at: http://bit.ly/psu-junit5-j1
– See the code samples at: http://bit.ly/psu-junit5-j1-code
● JUnit 5– Users guide Current Milestone, SNAPSHOT
– Javadocs Current Milestone, SNAPSHOT
– Code and issues Github
Junit 5: Features, Architecture, and Extensibility - http://bit.ly/psu-junit5-j1
References
● Articles– Voxxed “JUnit 5 – Setup”
– Voxxed “JUnit 5 – The Basics”
– Voxxed “JUnit 5 – Architecture”
– Voxxed “J Unit 5 – Extension Model”
● Presentations– vJUG “JUnit 5: Next Generation Testing on the JVM”