Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to JUnit5 #420

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies {

compile "org.mockito:mockito-core:2.23.0"

testCompile 'junit:junit:4.12'
testImplementation(platform('org.junit:junit-bom:5.7.1'))
testImplementation('org.junit.jupiter:junit-jupiter')
testCompile 'com.nhaarman:expect.kt:1.0.0'

testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
Expand All @@ -36,6 +37,10 @@ dependencies {
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
}

test {
useJUnitPlatform()
}

dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
Expand Down
3 changes: 1 addition & 2 deletions mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*


class CoroutinesTest {

@Test
Expand Down
10 changes: 9 additions & 1 deletion tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.mockito:mockito-core:2.23.0"

// only needed because "test.VerifyTest.verifyFailsWithWrongArg" checks for ArgumentsAreDifferent which inherits from JUnit 4 ComparisonFailure
testCompile "junit:junit:4.12"

testImplementation(platform("org.junit:junit-bom:5.7.1"))
testImplementation "org.junit.jupiter:junit-jupiter"
testCompile "com.nhaarman:expect.kt:1.0.0"
}
}

test {
useJUnitPlatform()
}
13 changes: 8 additions & 5 deletions tests/src/test/kotlin/test/ArgumentCaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package test

import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import org.junit.Test
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*
import java.util.*

Expand Down Expand Up @@ -179,7 +180,7 @@ class ArgumentCaptorTest : TestBase() {
}
}

@Test(expected = IndexOutOfBoundsException::class)
@Test
fun argumentCaptor_callPropertyNotAvailable() {
/* Given */
val m: Methods = mock()
Expand All @@ -188,10 +189,12 @@ class ArgumentCaptorTest : TestBase() {
m.int(1)

/* Then */
argumentCaptor<Int>().apply {
verify(m).int(capture())
Assertions.assertThrows(IndexOutOfBoundsException::class.java) {
argumentCaptor<Int>().apply {
verify(m).int(capture())

expect(secondValue).toBe(2)
expect(secondValue).toBe(2)
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions tests/src/test/kotlin/test/BDDMockitoTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package test

import com.nhaarman.expect.expect
import org.junit.Test
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*
import org.mockito.stubbing.Answer

Expand Down Expand Up @@ -106,14 +107,16 @@ class BDDMockitoTest {
expect(mock.stringResult("Test")).toBe("tseT")
}

@Test(expected = IllegalStateException::class)
@Test
fun given_willThrowInfix_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()) willThrow { IllegalStateException() }
mock.stringResult()
Assertions.assertThrows(IllegalStateException::class.java) {
given(mock.stringResult()) willThrow { IllegalStateException() }
mock.stringResult()
}
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions tests/src/test/kotlin/test/EqTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ package test
*/

import com.nhaarman.expect.expect
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.Mockito

class EqTest : TestBase() {
Expand All @@ -41,13 +41,13 @@ class EqTest : TestBase() {

private lateinit var doAnswer: Open

@Before
@BeforeEach
fun setup() {
/* Create a proper Mockito state */
doAnswer = Mockito.doAnswer { }.`when`(mock())
}

@After
@AfterEach
override fun tearDown() {
super.tearDown()

Expand Down
4 changes: 2 additions & 2 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatcher
import org.mockito.internal.matchers.VarargMatcher
import org.mockito.invocation.InvocationOnMock
Expand Down Expand Up @@ -331,4 +331,4 @@ class MatchersTest : TestBase() {

override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
}
}
}
4 changes: 2 additions & 2 deletions tests/src/test/kotlin/test/MockingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.exceptions.verification.WantedButNotInvoked
import org.mockito.listeners.InvocationListener
Expand Down Expand Up @@ -354,4 +354,4 @@ class MockingTest : TestBase() {

private interface MyInterface
private open class MyClass
}
}
5 changes: 2 additions & 3 deletions tests/src/test/kotlin/test/OngoingStubbingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package test
import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.expect.fail
import org.junit.Assume.assumeFalse
import org.junit.Test
import org.junit.jupiter.api.Assumptions.assumeFalse
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.exceptions.misusing.UnfinishedStubbingException
import org.mockito.kotlin.*
import org.mockito.stubbing.Answer
import kotlin.check

class OngoingStubbingTest : TestBase() {

Expand Down
15 changes: 9 additions & 6 deletions tests/src/test/kotlin/test/SpyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ package test
*/

import com.nhaarman.expect.expect
import org.junit.After
import org.junit.Test
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.kotlin.*
import java.util.*
Expand All @@ -38,7 +39,7 @@ class SpyTest : TestBase() {
private val openClassInstance: MyClass = MyClass()
private val closedClassInstance: ClosedClass = ClosedClass()

@After
@AfterEach
override fun tearDown() {
super.tearDown()
Mockito.validateMockitoUsage()
Expand Down Expand Up @@ -77,11 +78,13 @@ class SpyTest : TestBase() {
expect(date.time).toBe(0L)
}

@Test(expected = IllegalArgumentException::class)
@Test
fun doThrowWithSpy() {
val date = spy(Date(0))
doThrow(IllegalArgumentException()).whenever(date).time
date.time
Assertions.assertThrows(IllegalArgumentException::class.java) {
doThrow(IllegalArgumentException()).whenever(date).time
date.time
}
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test/kotlin/test/StubberTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*

class StubberTest : TestBase() {
Expand Down Expand Up @@ -100,4 +100,4 @@ class StubberTest : TestBase() {
mock.go()
}
}
}
}
6 changes: 3 additions & 3 deletions tests/src/test/kotlin/test/TestBase.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package test

import org.junit.After
import org.junit.jupiter.api.AfterEach

abstract class TestBase {

@After
@AfterEach
open fun tearDown() {
mockMakerInlineEnabled = null
}
}
}
4 changes: 2 additions & 2 deletions tests/src/test/kotlin/test/VerificationTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import com.nhaarman.expect.expect
import org.junit.Test
import org.junit.jupiter.api.Test
import org.mockito.exceptions.base.MockitoAssertionError
import org.mockito.kotlin.*
import org.mockito.kotlin.verify
Expand Down Expand Up @@ -112,4 +112,4 @@ class VerificationTest : TestBase() {
verify(this, after(10)).int(3)
}
}
}
}
21 changes: 13 additions & 8 deletions tests/src/test/kotlin/test/VerifyTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package test

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.junit.Test
import org.mockito.exceptions.verification.TooLittleActualInvocations
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent

Expand All @@ -30,25 +31,29 @@ class VerifyTest : TestBase() {
}
}

@Test(expected = TooLittleActualInvocations::class)
@Test
fun verifyFailsWithWrongCount() {
val iface = mock<TestInterface>()

iface.call(0)

verify(iface) {
2 * { call(0) }
Assertions.assertThrows(TooLittleActualInvocations::class.java) {
verify(iface) {
2 * { call(0) }
}
}
}

@Test(expected = ArgumentsAreDifferent::class)
@Test
fun verifyFailsWithWrongArg() {
val iface = mock<TestInterface>()

iface.call(3)

verify(iface) {
1 * { call(0) }
Assertions.assertThrows(ArgumentsAreDifferent::class.java) {
verify(iface) {
1 * { call(0) }
}
}
}

Expand Down Expand Up @@ -93,4 +98,4 @@ class VerifyTest : TestBase() {

fun defaultArgs(a: Int = 3, b: Int = 42)
}
}
}
3 changes: 1 addition & 2 deletions tests/src/test/kotlin/test/createinstance/NullCasterTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package test.createinstance

import com.nhaarman.expect.expect
import org.junit.jupiter.api.Test
import org.mockito.kotlin.internal.createInstance
import org.junit.Test
import test.TestBase


class NullCasterTest : TestBase() {

@Test
Expand Down
8 changes: 4 additions & 4 deletions tests/src/test/kotlin/test/inline/UsingMockMakerInlineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/

import com.nhaarman.expect.expect
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assumptions.assumeTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*
import test.mockMakerInlineEnabled
import java.io.IOException
Expand All @@ -43,7 +43,7 @@ class UsingMockMakerInlineTest {
}
}

@Before
@BeforeEach
fun setup() {
mockMakerInlineEnabled = null
assumeTrue(mockMakerInlineEnabled())
Expand Down