Skip to content

Commit eba5b93

Browse files
felipe-gdrmaibin
authored andcommitted
BAEL-809: Add Kotlin Mockito supporting code (eugenp#2102)
1 parent 0144471 commit eba5b93

9 files changed

Lines changed: 245 additions & 0 deletions

File tree

‎kotlin-mockito/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Relevant articles:
2+
3+
- [Introduction to the Kotlin Language](http://www.baeldung.com/kotlin)
4+
- [A guide to the “when{}” block in Kotlin](http://www.baeldung.com/kotlin-when)
5+
- [Comprehensive Guide to Null Safety in Kotlin](http://www.baeldung.com/kotlin-null-safety)
6+
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
7+
- [Difference Between “==” and “===” in Kotlin](http://www.baeldung.com/kotlin-equality-operators)
8+
- [Generics in Kotlin](http://www.baeldung.com/kotlin-generics)

‎kotlin-mockito/pom.xml‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>kotlin-mockito</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-modules</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.jetbrains.kotlin</groupId>
18+
<artifactId>kotlin-stdlib</artifactId>
19+
<version>${kotlin.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.mockito</groupId>
23+
<artifactId>mockito-all</artifactId>
24+
<version>${mockito.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>${junit.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
36+
<build>
37+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
38+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
39+
40+
<plugins>
41+
<plugin>
42+
<artifactId>kotlin-maven-plugin</artifactId>
43+
<groupId>org.jetbrains.kotlin</groupId>
44+
<version>${kotlin.version}</version>
45+
<executions>
46+
<execution>
47+
<id>compile</id>
48+
<goals>
49+
<goal>compile</goal>
50+
</goals>
51+
<configuration>
52+
<sourceDirs>
53+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
54+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
55+
</sourceDirs>
56+
</configuration>
57+
</execution>
58+
<execution>
59+
<id>test-compile</id>
60+
<goals>
61+
<goal>test-compile</goal>
62+
</goals>
63+
<configuration>
64+
<sourceDirs>
65+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
66+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
67+
</sourceDirs>
68+
</configuration>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-compiler-plugin</artifactId>
75+
<version>${maven-compiler.version}</version>
76+
<executions>
77+
<!-- Replacing default-compile as it is treated specially
78+
by maven -->
79+
<execution>
80+
<id>default-compile</id>
81+
<phase>none</phase>
82+
</execution>
83+
<!-- Replacing default-testCompile as it is treated specially
84+
by maven -->
85+
<execution>
86+
<id>default-testCompile</id>
87+
<phase>none</phase>
88+
</execution>
89+
<execution>
90+
<id>java-compile</id>
91+
<phase>compile</phase>
92+
<goals>
93+
<goal>compile</goal>
94+
</goals>
95+
</execution>
96+
<execution>
97+
<id>java-test-compile</id>
98+
<phase>test-compile</phase>
99+
<goals>
100+
<goal>testCompile</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
</plugins>
106+
</build>
107+
108+
<properties>
109+
<mockito.version>1.9.5</mockito.version>
110+
<junit.version>4.12</junit.version>
111+
<kotlin.version>1.1.2-4</kotlin.version>
112+
<maven-compiler.version>3.5.1</maven-compiler.version>
113+
</properties>
114+
115+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.java;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class ArrayExample {
8+
9+
public int sumValues(int[] nums) {
10+
int res = 0;
11+
12+
for (int x:nums) {
13+
res += x;
14+
}
15+
16+
return res;
17+
}
18+
19+
public void writeList() throws IOException {
20+
File file = new File("E://file.txt");
21+
FileReader fr = new FileReader(file);
22+
fr.close();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.java;
2+
3+
public class Customer {
4+
5+
private String firstName;
6+
private String lastName;
7+
8+
public String getFirstName() {
9+
return firstName;
10+
}
11+
12+
public void setFirstName(String firstName) {
13+
this.firstName = firstName;
14+
}
15+
16+
public String getLastName() {
17+
return lastName;
18+
}
19+
20+
public void setLastName(String lastName) {
21+
this.lastName = lastName;
22+
}
23+
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.java;
2+
3+
public class StringUtils {
4+
public static String toUpperCase(String name) {
5+
return name.toUpperCase();
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.kotlin
2+
3+
interface BookService {
4+
fun inStock(bookId: Int): Boolean
5+
fun lend(bookId: Int, memberId: Int)
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.kotlin
2+
3+
class LendBookManager(val bookService:BookService) {
4+
fun checkout(bookId: Int, memberId: Int) {
5+
if(bookService.inStock(bookId)) {
6+
bookService.lend(bookId, memberId)
7+
} else {
8+
throw IllegalStateException("Book is not available")
9+
}
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.kotlin;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class JavaCallToKotlinUnitTest {
8+
@Test
9+
public void givenKotlinClass_whenCallFromJava_shouldProduceResults() {
10+
//when
11+
int res = new MathematicsOperations().addTwoNumbers(2, 4);
12+
13+
//then
14+
assertEquals(6, res);
15+
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.kotlin;
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
import java.lang.IllegalStateException
6+
import org.mockito.InjectMocks
7+
import org.mockito.Mockito
8+
9+
class LibraryManagementTest {
10+
@Test(expected = IllegalStateException::class)
11+
fun whenBookIsNotAvailable_thenAnExceptionIsThrown() {
12+
val mockBookService = Mockito.mock(BookService::class.java)
13+
14+
Mockito.`when`(mockBookService.inStock(100)).thenReturn(false)
15+
16+
val manager = LendBookManager(mockBookService)
17+
18+
manager.checkout(100, 1)
19+
}
20+
21+
@Test
22+
fun whenBookIsAvailable_thenLendMethodIsCalled() {
23+
val mockBookService = Mockito.mock(BookService::class.java)
24+
25+
Mockito.`when`(mockBookService.inStock(100)).thenReturn(true)
26+
27+
val manager = LendBookManager(mockBookService)
28+
29+
manager.checkout(100, 1)
30+
31+
Mockito.`verify`(mockBookService).lend(100, 1)
32+
}
33+
}

0 commit comments

Comments
 (0)