forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-809: Add Kotlin Mockito supporting code (eugenp#2102)
- Loading branch information
1 parent
0144471
commit eba5b93
Showing
9 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
24 changes: 24 additions & 0 deletions
24
kotlin-mockito/src/main/java/com/baeldung/java/ArrayExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
kotlin-mockito/src/main/java/com/baeldung/java/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
kotlin-mockito/src/main/java/com/baeldung/java/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.baeldung.java; | ||
|
||
public class StringUtils { | ||
public static String toUpperCase(String name) { | ||
return name.toUpperCase(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
kotlin-mockito/src/main/kotlin/com/baeldung/kotlin/BookService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.kotlin | ||
|
||
interface BookService { | ||
fun inStock(bookId: Int): Boolean | ||
fun lend(bookId: Int, memberId: Int) | ||
} |
11 changes: 11 additions & 0 deletions
11
kotlin-mockito/src/main/kotlin/com/baeldung/kotlin/LendBookManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
kotlin-mockito/src/test/java/com/baeldung/kotlin/JavaCallToKotlinUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |