Skip to content

Commit 39e786c

Browse files
Added Kotlin examples with JUnit5 and Mockito
1 parent 88bfac2 commit 39e786c

34 files changed

+813
-59
lines changed

Tutorial3-1JUnit5/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ dependencies {
3737
// Mockito
3838
testImplementation 'org.mockito:mockito-core:3.0.0'
3939

40+
// Mockito for JUnit 5
41+
testImplementation 'org.mockito:mockito-junit-jupiter:3.0.0'
42+
4043
// Hamcrest
4144
testImplementation "org.hamcrest:hamcrest-all:$hamcrestVersion"
4245

Tutorial3-1JUnit5/src/test/java/com/smarttoolfactory/tutorial3_1_junit5/NestedTestSimpleExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.junit.jupiter.api.Nested;
88
import org.junit.jupiter.api.Test;
99

10-
public class NestedTestSimpleExample {
10+
class NestedTestSimpleExample {
1111

1212
@BeforeAll
1313
static void setUpBeforeClass() throws Exception {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*/
1111
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
12-
class Test9Order1OrderAnnotation {
12+
class Test10Order1OrderAnnotation {
1313

1414

1515
@Test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Tests run in order with the name given to methods based on alphanumeric ordering
1010
*/
1111
@TestMethodOrder(MethodOrderer.Alphanumeric.class)
12-
class Test9Order2AlphaNumeric {
12+
class Test10Order2AlphaNumeric {
1313

1414
@Test
1515
void testC() {

Tutorial3-1JUnit5/src/test/java/com/smarttoolfactory/tutorial3_1_junit5/Test2DisplayAndDisabled.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
*
1111
* <p></p>
1212
* Disabled test are ignored.
13-
*
1413
*/
15-
public class Test2DisplayAndDisabled {
14+
class Test2DisplayAndDisabled {
1615

1716

1817
@DisplayName("Single test successful")

Tutorial3-1JUnit5/src/test/java/com/smarttoolfactory/tutorial3_1_junit5/Test3Assertions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* N/A assertTimeOut
3232
* N/A assertTimeoutPreemptively
3333
*/
34-
public class Test3Assertions {
34+
class Test3Assertions {
3535

3636
private final Calculator calculator = new Calculator();
3737

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.smarttoolfactory.tutorial3_1_junit5;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
class Test4Exception {
10+
11+
@Test
12+
void whenExceptionThrown_thenAssertionSucceeds() {
13+
14+
Exception exception = assertThrows(NumberFormatException.class, () -> Integer.parseInt("1a"));
15+
16+
String expectedMessage = "For input string";
17+
String actualMessage = exception.getMessage();
18+
19+
assertTrue(actualMessage.contains(expectedMessage));
20+
}
21+
22+
@Test
23+
void test_exception() {
24+
25+
Exception exception = assertThrows(
26+
ArithmeticException.class,
27+
() -> divide(1, 0));
28+
29+
assertEquals("/ by zero", exception.getMessage());
30+
31+
assertTrue(exception.getMessage().contains("zero"));
32+
33+
}
34+
35+
int divide(int input, int divide) {
36+
return input / divide;
37+
}
38+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import static org.junit.jupiter.params.provider.Arguments.arguments;
2727
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;
2828

29-
class Test4Parameterized {
29+
class Test5ParameterizedTests {
3030

3131
/*
3232
@ValueSource
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* <p></p>
2020
* Assumptions also understand lambda expressions.
2121
*/
22-
class Test5Assumptions {
22+
class Test6Assumptions {
2323

2424
@Test
2525
void testOnDev() {
@@ -31,7 +31,7 @@ void testOnDev() {
3131
@Test
3232
void testOnProd() {
3333
System.setProperty("ENV", "PROD");
34-
assumeTrue("DEV".equals(System.getProperty("ENV")), Test5Assumptions::message);
34+
assumeTrue("DEV".equals(System.getProperty("ENV")), Test6Assumptions::message);
3535
//remainder of test will be aborted
3636
}
3737

Tutorial3-1JUnit5/src/test/java/com/smarttoolfactory/tutorial3_1_junit5/Test6Nested.java renamed to Tutorial3-1JUnit5/src/test/java/com/smarttoolfactory/tutorial3_1_junit5/Test7NestedTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.junit.jupiter.api.Test;
1515

1616
@DisplayName("A stack")
17-
class Test6Nested {
17+
class Test7NestedTests {
1818

1919
Stack<Object> stack;
2020

0 commit comments

Comments
 (0)