22

Automated Testing for Digital Experiences Using JUnit 5

Embed Size (px)

Citation preview

© 2016 Pivotal!2

Automated Testing for Digital Experiences

Mike Koleno, CTO, Solstice

With JUnit 5

PROPRIETARY & CONFIDENTIAL

3 solstice.com

ABOUT ME• My name is Mike Koleno • CTO & Head of Enterprise Cloud Computing at Solstice • Enterprise developer & architect for 15+ years

• Consulted on behalf of 40+ Fortune 1000 enterprises

4

PROPRIETARY & CONFIDENTIAL

solstice.com

Reimagine Digital Customer Experiences

Design and Build New Digital Products

Help with Digital Operations

1 2 3

DIGITAL INNOVATION FIRM

PROPRIETARY & CONFIDENTIAL

5 solstice.com

AGENDA• It’s All About the Tests • Where we’ve been - through the lens of Java/Spring & JUnit 4 • Where we’re going - JUnit 5 • Slackbot demo

PROPRIETARY & CONFIDENTIAL

6 solstice.com

IT’S ALL ABOUT THE TESTS

Tests Have Expanded

Unit Tests have grown into Integration Tests, Functional Tests and

System Tests

Dev & Build Tools

Tools are constantly adding new features for writing/running

tests

TDD & CI/CD

Modern Software Engineering techniques highly value automated

tests

7

PROPRIETARY & CONFIDENTIAL

solstice.com

2006 - JUnit 4 introduced

2007 - First iPhone released

2014 - Java 8 released

2013 - Slack launched

2008 - first GitHub repo

PROPRIETARY & CONFIDENTIAL

8 solstice.com

JUNIT 4 - THE GAPS

Any Day Now…

JUnit 4 built on Java 5, unable to make

use of modern language features

Developer Friction

Rules, Runner restrictions, etc.

Tight Coupling

Test Writing API coupled with Test

Discovery & Execution API’s

JUNIT 5

9

PROPRIETARY & CONFIDENTIAL

solstice.com

PROPRIETARY & CONFIDENTIAL

10 solstice.com

MODULARIZING JUNIT

= JUnit PlatformAPI for Dev & Build Tools

Test discovery and execution

JUnit Jupiter+Contains the developer API’s to write tests

JUnit Vintage+Ability to run legacy tests (JUnit 3 & JUnit 4)

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

11 solstice.com

LAMBDA SUPPORT@Testvoid lambaTest() {    List<Integer> numbers = Arrays.asList(1, 2, 3);    assertTrue(numbers        .stream()        .mapToInt(i -> i)        .sum() > 5, () -> "Sum should be greater than 5");}

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

12 solstice.com

EXTENSION MODEL• Allows developers to hook into different test life cycle callback events via an extension

• BeforeAllCallback, BeforeEachCallback, BeforeTestExecutionCallback • AfterTestExecutionCallback, AfterEachCallback, AfterAllCallback

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

13 solstice.com

EXTENSION MODEL (CONT)

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

14 solstice.com

PARAMETERS

@ExtendWith(MockedSystemExtension.class)public class SystemTest {

@Test public void test(final MockedSystem mockedSystem) { final Response response = mockedSystem.target().request().get();

assertEquals(response.getStatusInfo().getFamily(), Response.Status.Family.SUCCESSFUL, () -> "status code is not 2xx"); assertEquals(response.getHeaderString("X-Hello"), "World", () -> "header 'X-Hello' not correct"); }

}

• Allows developers to pass parameters into tests

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

15 solstice.com

PARAMETERS (CONT)public class MockedSystemExtension implements ParameterResolver {

@Override public boolean supports(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException { return parameterContext.getParameter().getType().isAssignableFrom(MockedSystem.class); }

@Override public Object resolve(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException { final MockedSystem mockedSystem = new MockedSystem();

return mockedSystem; }

}

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

@TestFactory default Collection<DynamicTest> dynamicTestsFromCollection() { return Arrays.asList( dynamicTest("1st dynamic test in test interface", () -> assertTrue(true)), dynamicTest("2nd dynamic test in test interface", () -> assertEquals(4, 2 * 2)) ); }

PROPRIETARY & CONFIDENTIAL

16 solstice.com

DYNAMIC TESTS

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

17 solstice.com

NEW TEST CONVENTIONSPARAMETERIZED TESTS@ParameterizedTest@ValueSource(ints = { 1, 2, 3 })void testWithValueSource(int argument) { assertNotNull(argument);}

@RepeatedTest(10)void repeatedTest() { // ...}

REPEATED TESTS

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

18 solstice.com

OTHER CHANGES - DISABLING TESTS The @Disabled annotation was added in order to disable test cases and individual tests

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

19 solstice.com

OTHER CHANGES - DISPLAY NAME The @DisplayName annotation can be used to provide better description names to your tests

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

20 solstice.com

OTHER CHANGES - @TAG @Tags have replaced @Categories in order to group and filter tests

ARCHITECTURE EXTENSIBILITY

NEW TESTS ANNOTATIONS

PROPRIETARY & CONFIDENTIAL

21 solstice.com

SLACKBOT DEMO ARCHITECTURE

STAY IN TOUCH

22

PROPRIETARY & CONFIDENTIAL

solstice.com

Mike Koleno [email protected]

solstice.com

Slackbot demo: bit.ly/2r8Ctlf