Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hello New World – Junit5 ❤️ Kotlin

Avatar for MobiTalk MobiTalk
November 27, 2019

Hello New World – Junit5 ❤️ Kotlin

Talk on Junit5 features and using test first approach for learning Kotlin.

Avatar for MobiTalk

MobiTalk

November 27, 2019

More Decks by MobiTalk

Other Decks in Programming

Transcript

  1. Hello New World – Junit5 ❤ Kotlin • Areef Shaik

    • MobiTalk Nov Meetup © 2019 PayPal Inc.
  2. Learning Kotlin the new Agile way! UI Tests Integration Tests

    Unit Tests Ref: https://martinfowler.com/tags/test%20categories.html
  3. A brief history of JUnit! Kent Beck writes SUnit (and

    caught the attention of smalltalkers!) Kent Beck and Erich Gamma ports SUnit to Java - JUnit Was Born (and the world was changed!) JUnit4.12 was released (and stayed there forever!) JUnit Lambda crowdfunding on Indiegogo (and the evolution begins!) 2016 2014 1997 1994
  4. Rewrite: Why? • IDE / Tools integration • Integrations tightly

    coupled to JUnit internals by using internal classes or reflection • The execution mode • All test cases need to be known before run • Ambiguous Evolution • Runner vs Rule : conflicting conceptions for test execution
  5. • Complete rewrite of JUnit • Improving on what JUnit

    4 had to offer with extensibility in mind • Modular, Extensible and Modern • Support for any TestEngine imaginable • Forward and backward compatible • JUnit Platform supports JUnit 3.8, JUnit 4, and JUnit Jupiter What is JUnit5?
  6. JUnit5 On Android • JUnit 5 Gradle Plugin only works

    in pure-Java projects only (As Android uses it’s own plug-in) • For Android JUnit5 support third party plug-in written by Marcel Schnelle • Full support for Unit tests with experimental support for Instrumentation tests
  7. JUnit Jupiter - The new programming model and extension model

    in JUnit 5 • Programming Model • How you write tests • Annotations • Assertions • Assumptions • Types of tests • Extension Model • How you and third parties extend the framework • Spring, Mockito, Selenium, …
  8. JUnit Jupiter – Annotations Comparison with JUnit4 JUnit 4 JUnit

    Jupiter @org.junit.Test @org.junit.jupiter.api.Test @Ignore @Disabled @BeforeClass / @AfterClass @BeforeAll / @AfterAll @Before / @After @BeforeEach / @AfterEach org.junit.Assert org.junit.jupiter.api.Assertions org.junit.Assume org.junit.jupiter.api.Assumptions
  9. JUnit Jupiter Multi Assertions class AssertAllTests { @Test fun multipleAssertionsTests()

    { assertAll("All must pass!", { assertTrue(true) }, { assertEquals(1+1, 2) }, { assertFalse(false)} ) } }
  10. JUnit Jupiter Assumptions class AssumptionTests { @Test fun assumeTests() {

    assumeTrue(true) assertEquals(8 / 2, 4) } } class AssumptionTests { @Test fun assumeTests() { assumeTrue(false) assertEquals(8 / 2, 4) } }
  11. JUnit Jupiter New Annotations – @DisplayName(String) @DisplayName("MobiTalk Junit5 - DisplayName

    Demo ") class DisplayNameTests { @DisplayName("First test in Junit5") @Test fun firstTest() { assertTrue { true } } }
  12. JUnit Jupiter New Annotations – @Enabled / @Disabled @Test @EnabledOnOs(MAC)

    fun onlyRunThisOnMac() { assertTrue(System.getProperty("user.home").startsWith("/ Users")) } @Test @DisabledOnOs(MAC) fun neverRunThisOnMac() { val osName = System.getProperty("os.name").toLowerCase(Locale.ROOT) assertFalse("darwin" in osName) assertFalse("mac" in osName) }
  13. JUnit Jupiter New Annotations – @Nested @DisplayName("Given a Calculator") class

    CalculatorTests { lateinit var calculator: Calculator @BeforeEach fun beforeEach() { this.calculator = Calculator() } @Nested @DisplayName("When Performing Addition") inner class AddOperation { @Test @DisplayName("Then Computing 1 and 2 equals 3") fun oneAndTwoIsThree() { val actual = calculator.add(1, 2) assertEquals(3, actual) } } }
  14. JUnit Jupiter New Annotations – @TestFactory class FactoryTests { @TestFactory

    fun createDynamicTests(): List<DynamicTest> = listOf( dynamicTest("A Dynamic Test") { assertEquals(4, 2 + 2) }, dynamicTest("Another One") { assertEquals(0, 2 - 2) } ) }
  15. JUnit Jupiter New Annotations – @ParameterizedTest & <Source> class ParameterizedTests

    { @ParameterizedTest @CsvSource("Alice, 5", "Bob, 3", "Charles, 7") fun expectedNameLengths(name: String, expectedLen: Int) { assertEquals(expectedLen, name.length) } }
  16. JUnit Jupiter New Annotations – @ParameterizedTest & <Source> Different possibilities

    for parameter providers: • @ValueSource: char, short, byte, int, long, float, double, String, Class • @NullSource, @EmptySource, and @NullAndEmptySource • @EnumSource • @MethodSource • @CsvSource & @CsvFileSource • @ArgumentsSource & custom ArgumentsProvider (write your own)
  17. JUnit Jupiter New Annotations – @RepeatedTest class RepeatTests { @RepeatedTest(5)

    fun repeatedTest(repetitionInfo: RepetitionInfo ) { assertEquals(5, repetitionInfo.totalRepetitions) } }
  18. JUnit Jupiter New Annotations – @RepeatedTest class RepeatWithNameTests { @RepeatedTest(

    value = 5, name = "I am at {currentRepetition} of {totalRepetitions}" ) fun repeatedTest(repetitionInfo: RepetitionInfo ) { assertEquals(5, repetitionInfo.totalRepetitions) } }
  19. References • Junit5 Official • https://junit.org/junit5/docs/current/user-guide/ • https://github.com/junit-team/junit5 • https://github.com/junit-team/junit5-samples

    • Marcel Schnelle • https://github.com/mannodermaus/android-junit5 • https://github.com/CasterIO/junit5-introduction-examples • TheBowlingGameKata • http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata • http://butunclebob.com/files/downloads/Bowling%20Game%20Kata.ppt
  20. Thank you !! (Once again :P) • Areef Shaik –

    PayPal India • https://www.linkedin.com/in/areefhussain/ • https://twitter.com/sm_areef