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

Reduce warnings during build, in IDE and in GitHub UI #501

Merged
merged 7 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 32 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# SINGLE-JOB
#
verify:
runs-on: ubuntu-latest
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"

steps:
Expand All @@ -44,7 +44,7 @@ jobs:
#
build:
needs: [verify]
runs-on: ubuntu-latest
runs-on: ubuntu-latest

# Definition of the build matrix
strategy:
Expand All @@ -56,20 +56,20 @@ jobs:

steps:

- name: 1. Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout
- name: 1. Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout

- name: 2. Set up Java 11
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
with:
java-version: 11
- name: 2. Set up Java 11
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
with:
java-version: 11

- name: 3. Build with Kotlin ${{ matrix.kotlin }} and mock-maker ${{ matrix.mock-maker }}
run: |
ops/mockMakerInline.sh
./gradlew build -PtestKotlinVersion=${{ matrix.kotlin }}
env:
MOCK_MAKER: ${{ matrix.mock-maker }}
- name: 3. Build with Kotlin ${{ matrix.kotlin }} and mock-maker ${{ matrix.mock-maker }}
run: |
ops/mockMakerInline.sh
./gradlew build -PtestKotlinVersion=${{ matrix.kotlin }}
env:
MOCK_MAKER: ${{ matrix.mock-maker }}

#
# Release job, only for pushes to the main development branch
Expand All @@ -85,21 +85,21 @@ jobs:

steps:

- name: Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout
with:
fetch-depth: '0' # https://github.com/shipkit/shipkit-changelog#fetch-depth-on-ci

- name: Set up Java 11
uses: actions/setup-java@v1
with:
java-version: 11

- name: Build and release
run: ./gradlew githubRelease publishToSonatype closeAndReleaseStagingRepository releaseSummary
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
NEXUS_TOKEN_USER: ${{secrets.NEXUS_TOKEN_USER}}
NEXUS_TOKEN_PWD: ${{secrets.NEXUS_TOKEN_PWD}}
PGP_KEY: ${{secrets.PGP_KEY}}
PGP_PWD: ${{secrets.PGP_PWD}}
- name: Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout
with:
fetch-depth: '0' # https://github.com/shipkit/shipkit-changelog#fetch-depth-on-ci

- name: Set up Java 11
uses: actions/setup-java@v1
with:
java-version: 11

- name: Build and release
run: ./gradlew githubRelease publishToSonatype closeAndReleaseStagingRepository releaseSummary
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
NEXUS_TOKEN_USER: ${{secrets.NEXUS_TOKEN_USER}}
NEXUS_TOKEN_PWD: ${{secrets.NEXUS_TOKEN_PWD}}
PGP_KEY: ${{secrets.PGP_KEY}}
PGP_PWD: ${{secrets.PGP_PWD}}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Mockito-Kotlin

[ ![Download](https://maven-badges.herokuapp.com/maven-central/org.mockito.kotlin/mockito-kotlin/badge.svg) ](https://maven-badges.herokuapp.com/maven-central/org.mockito.kotlin/mockito-kotlin)
[![Nexus Snapshot](https://img.shields.io/nexus/s/org.mockito.kotlin/mockito-kotlin?server=https%3A%2F%2Fs01.oss.sonatype.org%2F)](https://s01.oss.sonatype.org/content/repositories/snapshots/org/mockito/kotlin/mockito-kotlin/)

Expand Down Expand Up @@ -56,7 +57,7 @@ keeping the base module at a recent version.

- `./gradlew check` runs the checks including tests.

Usually it is enough to test only using the default Kotlin versions;
Usually it is enough to test only using the default Kotlin versions;
CI will test against multiple versions.
If you want to test using a different Kotlin version locally,
add the `-PtestKotlinVersion=1.2.3` argument to the Gradle invocation while running the tests.
Expand Down
4 changes: 2 additions & 2 deletions mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'com.nhaarman:expect.kt:1.0.1'

testImplementation "org.jetbrains.kotlin:kotlin-stdlib"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-stdlib"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'

testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class ArgumentCaptorHolder5<out A, out B, out C, out D, out E>(
operator fun component5() = fifth
}


/**
* Creates 4 [KArgumentCaptor]s for given types.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package org.mockito.kotlin
import org.mockito.InOrder
import org.mockito.verification.VerificationMode

interface KInOrder: InOrder {
interface KInOrder : InOrder {
/**
* Verifies certain suspending behavior <b>happened once</b> in order.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
import kotlin.reflect.KClass


inline fun <T : Any> stubbing(
mock: T,
stubbing: KStubbing<T>.(T) -> Unit
Expand All @@ -47,7 +46,7 @@ inline fun <T : Any> T.stub(stubbing: KStubbing<T>.(T) -> Unit): T {

class KStubbing<out T : Any>(val mock: T) {
init {
if(!mockingDetails(mock).isMock) throw NotAMockException("Stubbing target is not a mock!")
if (!mockingDetails(mock).isMock) throw NotAMockException("Stubbing target is not a mock!")
}

fun <R> on(methodCall: R): OngoingStubbing<R> = Mockito.`when`(methodCall)
Expand Down Expand Up @@ -90,4 +89,4 @@ class KStubbing<out T : Any>(val mock: T) {
fun Stubber.on(methodCall: T.() -> Unit) {
this.`when`(mock).methodCall()
}
}
}
5 changes: 2 additions & 3 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ inline fun <reified T : Any> anyVararg(): T {
}

fun <T : Any> anyVararg(clazz: KClass<T>): T {
return ArgumentMatchers.argThat(VarargMatcher(clazz.java))?: createInstance(clazz)
return ArgumentMatchers.argThat(VarargMatcher(clazz.java)) ?: createInstance(clazz)
}

private class VarargMatcher<T>(private val clazz: Class<T>) : ArgumentMatcher<T>{
private class VarargMatcher<T>(private val clazz: Class<T>) : ArgumentMatcher<T> {
override fun matches(t: T): Boolean = true

// In Java >= 12 you can do clazz.arrayClass()
Expand Down Expand Up @@ -148,4 +148,3 @@ fun <T : Any> notNull(): T? {
inline fun <reified T : Any> refEq(value: T, vararg excludeFields: String): T {
return ArgumentMatchers.refEq<T>(value, *excludeFields) ?: createInstance()
}

3 changes: 2 additions & 1 deletion mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Mocking.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.mockito.MockSettings
import org.mockito.Mockito
import org.mockito.listeners.InvocationListener
import org.mockito.mock.SerializableMode
import org.mockito.quality.Strictness
import org.mockito.stubbing.Answer
import kotlin.DeprecationLevel.ERROR
import kotlin.reflect.KClass
Expand Down Expand Up @@ -175,7 +176,7 @@ fun withSettings(
if (stubOnly) stubOnly()
useConstructor?.let { useConstructor(*it.args) }
outerInstance?.let { outerInstance(it) }
if (lenient) lenient()
if (lenient) strictness(Strictness.LENIENT)
}

class UseConstructor private constructor(val args: Array<Any>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ fun <T> reset(vararg mocks: T) {

fun mockingDetails(toInspect: Any): MockingDetails {
return Mockito.mockingDetails(toInspect)!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
import kotlin.reflect.KClass


/**
* Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.
*
Expand Down
5 changes: 2 additions & 3 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Spying.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package org.mockito.kotlin

import org.mockito.Mockito


/**
* Creates a spy of the real object.
* The spy calls <b>real</b> methods unless they are stubbed.
Expand Down Expand Up @@ -58,5 +57,5 @@ fun <T> spy(value: T): T {
*/
inline fun <reified T : Any> spy(value: T, stubbing: KStubbing<T>.(T) -> Unit): T {
return spy(value)
.apply { KStubbing(this).stubbing(this) }!!
}
.apply { KStubbing(this).stubbing(this) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Stubber
import kotlin.reflect.KClass


fun <T> doAnswer(answer: (InvocationOnMock) -> T?): Stubber {
return Mockito.doAnswer { answer(it) }!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package org.mockito.kotlin

import org.mockito.kotlin.internal.createInstance
import kotlinx.coroutines.runBlocking
import org.mockito.InOrder
import org.mockito.Mockito
import org.mockito.kotlin.internal.KInOrderDecorator
import org.mockito.verification.VerificationAfterDelay
Expand Down Expand Up @@ -64,6 +63,7 @@ fun <T> verifyBlocking(mock: T, mode: VerificationMode, f: suspend T.() -> Unit)
val m = Mockito.verify(mock, mode)
runBlocking { m.f() }
}

/**
* Verifies certain behavior happened at least once / exact number of times / never.
*
Expand Down Expand Up @@ -259,7 +259,6 @@ fun only(): VerificationMode {
return Mockito.only()!!
}


/**
* For usage with verification only.
*
Expand All @@ -271,11 +270,12 @@ fun only(): VerificationMode {
inline fun <reified T : Any> check(noinline predicate: (T) -> Unit): T {
return Mockito.argThat { arg: T? ->
if (arg == null) error(
"""The argument passed to the predicate was null.

If you are trying to verify an argument to be null, use `isNull()`.
If you are using `check` as part of a stubbing, use `argThat` or `argForWhich` instead.
""".trimIndent()
"""
The argument passed to the predicate was null.

If you are trying to verify an argument to be null, use `isNull()`.
If you are using `check` as part of a stubbing, use `argThat` or `argForWhich` instead.
""".trimIndent()
)

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package org.mockito.kotlin


/**
* Verify multiple calls on mock
* Supports an easier to read style of
Expand All @@ -45,4 +44,4 @@ class VerifyScope<out T>(val mock: T) {
operator inline fun Int.times(call: T.() -> Unit) {
verify(mock, org.mockito.kotlin.times(this)).call()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package org.mockito.kotlin.internal

import kotlin.reflect.KClass
import java.lang.reflect.Array as JavaArray

inline fun <reified T : Any> createInstance(): T {
return when (T::class) {
Expand All @@ -42,7 +41,7 @@ inline fun <reified T : Any> createInstance(): T {
}
}

fun <T : Any> createInstance(kClass: KClass<T>): T {
fun <T : Any> createInstance(@Suppress("UNUSED_PARAMETER") kClass: KClass<T>): T {
return castNull()
}

Expand Down
1 change: 0 additions & 1 deletion mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.mockito.InOrder
import org.mockito.kotlin.*
import java.util.*


class CoroutinesTest {

@Test
Expand Down
1 change: 0 additions & 1 deletion tests/src/test/kotlin/test/LenientStubberTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.mockito.quality.Strictness


open class LenientStubberTest {
@get:Rule
val rule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS)
Expand Down
19 changes: 9 additions & 10 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.*
import org.mockito.stubbing.Answer
import java.io.IOException
import kotlin.reflect.KClass

class MatchersTest : TestBase() {

Expand Down Expand Up @@ -99,6 +98,7 @@ class MatchersTest : TestBase() {
verify(this).boolean(anyOrNull())
}
}

@Test
fun anyNull_forPrimitiveByte() {
mock<Methods>().apply {
Expand Down Expand Up @@ -238,7 +238,6 @@ class MatchersTest : TestBase() {
}
}


@Test
fun isA_withNonNullableString() {
mock<Methods>().apply {
Expand Down Expand Up @@ -310,14 +309,14 @@ class MatchersTest : TestBase() {
/** https://github.com/nhaarman/mockito-kotlin/issues/328 */
@Test
fun testRefEqForNonNullableParameter() {
mock<Methods>().apply {
/* When */
val array = intArrayOf(2, 3)
intArray(array)
/* Then */
verify(this).intArray(refEq(array))
}
mock<Methods>().apply {
/* When */
val array = intArrayOf(2, 3)
intArray(array)

/* Then */
verify(this).intArray(refEq(array))
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/kotlin/test/MockMaker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package test
import org.mockito.internal.configuration.plugins.Plugins
import org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker


internal var mockMakerInlineEnabled: Boolean? = null

internal fun mockMakerInlineEnabled(): Boolean {
return mockMakerInlineEnabled ?: (Plugins.getMockMaker() is InlineByteBuddyMockMaker)
}