BAEL-809: Add Kotlin Mockito supporting code (#2102)

This commit is contained in:
Felipe Reis 2017-06-18 16:39:30 +10:00 committed by maibin
parent 0144471d0f
commit eba5b938f5
9 changed files with 245 additions and 0 deletions

8
kotlin-mockito/README.md Normal file
View File

@ -0,0 +1,8 @@
## Relevant articles:
- [Introduction to the Kotlin Language](http://www.baeldung.com/kotlin)
- [A guide to the “when{}” block in Kotlin](http://www.baeldung.com/kotlin-when)
- [Comprehensive Guide to Null Safety in Kotlin](http://www.baeldung.com/kotlin-null-safety)
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
- [Difference Between “==” and “===” in Kotlin](http://www.baeldung.com/kotlin-equality-operators)
- [Generics in Kotlin](http://www.baeldung.com/kotlin-generics)

115
kotlin-mockito/pom.xml Normal file
View File

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>kotlin-mockito</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<executions>
<!-- Replacing default-compile as it is treated specially
by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially
by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<mockito.version>1.9.5</mockito.version>
<junit.version>4.12</junit.version>
<kotlin.version>1.1.2-4</kotlin.version>
<maven-compiler.version>3.5.1</maven-compiler.version>
</properties>
</project>

View File

@ -0,0 +1,24 @@
package com.baeldung.java;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ArrayExample {
public int sumValues(int[] nums) {
int res = 0;
for (int x:nums) {
res += x;
}
return res;
}
public void writeList() throws IOException {
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
fr.close();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.java;
public class Customer {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.java;
public class StringUtils {
public static String toUpperCase(String name) {
return name.toUpperCase();
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.kotlin
interface BookService {
fun inStock(bookId: Int): Boolean
fun lend(bookId: Int, memberId: Int)
}

View File

@ -0,0 +1,11 @@
package com.baeldung.kotlin
class LendBookManager(val bookService:BookService) {
fun checkout(bookId: Int, memberId: Int) {
if(bookService.inStock(bookId)) {
bookService.lend(bookId, memberId)
} else {
throw IllegalStateException("Book is not available")
}
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.kotlin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JavaCallToKotlinUnitTest {
@Test
public void givenKotlinClass_whenCallFromJava_shouldProduceResults() {
//when
int res = new MathematicsOperations().addTwoNumbers(2, 4);
//then
assertEquals(6, res);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.kotlin;
import org.junit.Assert
import org.junit.Test
import java.lang.IllegalStateException
import org.mockito.InjectMocks
import org.mockito.Mockito
class LibraryManagementTest {
@Test(expected = IllegalStateException::class)
fun whenBookIsNotAvailable_thenAnExceptionIsThrown() {
val mockBookService = Mockito.mock(BookService::class.java)
Mockito.`when`(mockBookService.inStock(100)).thenReturn(false)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
}
@Test
fun whenBookIsAvailable_thenLendMethodIsCalled() {
val mockBookService = Mockito.mock(BookService::class.java)
Mockito.`when`(mockBookService.inStock(100)).thenReturn(true)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
Mockito.`verify`(mockBookService).lend(100, 1)
}
}