Skip to content

Commit ae1d863

Browse files
authored
Merge pull request #119 from nhaarman/clean-tests
Use same test source for inline tests
2 parents 2b31ed7 + ab1eb0d commit ae1d863

File tree

16 files changed

+82
-52
lines changed

16 files changed

+82
-52
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ language: java
55
jdk:
66
- oraclejdk7
77

8+
matrix:
9+
include:
10+
- jdk: oraclejdk7
11+
env: TERM=dumb MOCK_MAKER=mock-maker-inline
12+
813
env:
914
matrix:
1015
- TERM=dumb

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ plugins {
33
}
44

55
apply from: 'gradle/scripts/tagging.gradle'
6+
7+
println ext.versionName

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
#
2525

2626
isRelease = false
27+
publishToLocal = false

mockito-kotlin/build.gradle

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,6 @@ repositories {
2121
jcenter()
2222
}
2323

24-
sourceSets {
25-
testInlineMockito {
26-
compileClasspath += main.output + test.output
27-
runtimeClasspath += main.output + test.output
28-
}
29-
}
30-
31-
configurations {
32-
testInlineMockitoCompile.extendsFrom testCompile
33-
testInlineMockitoRuntime.extendsFrom testRuntime
34-
}
35-
36-
// define custom test task for running integration tests
37-
task testInlineMockito(type: Test) {
38-
testClassesDir = sourceSets.testInlineMockito.output.classesDir
39-
classpath = sourceSets.testInlineMockito.runtimeClasspath
40-
}
41-
42-
test.dependsOn testInlineMockito
43-
44-
4524
dependencies {
4625
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
4726
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
@@ -63,3 +42,26 @@ dokka {
6342
}
6443
}
6544
javadoc.dependsOn dokka
45+
46+
//Switch inline
47+
task createTestResources << {
48+
def mockMakerFile = new File("$projectDir/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker")
49+
if (System.env.MOCK_MAKER != null) {
50+
logger.warn("! Using MockMaker ${System.env.MOCK_MAKER}")
51+
mockMakerFile.parentFile.mkdirs()
52+
mockMakerFile.createNewFile()
53+
mockMakerFile.write(System.env.MOCK_MAKER)
54+
} else {
55+
logger.warn("! Using default MockMaker")
56+
}
57+
}
58+
59+
task removeTestResources << {
60+
def mockMakerFile = new File("$projectDir/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker")
61+
if (mockMakerFile.exists()) {
62+
mockMakerFile.delete()
63+
}
64+
}
65+
66+
test.dependsOn createTestResources
67+
test.finalizedBy removeTestResources

mockito-kotlin/publishing.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ uploadArchives {
4040
)
4141
}
4242

43+
if (publishToLocal) repository(url: mavenLocal().url)
44+
4345
pom.project {
4446
name 'Mockito-Kotlin'
4547
packaging 'jar'

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ import java.lang.reflect.Array as JavaArray
4848
/**
4949
* Checks whether the resource file to enable mocking of final classes is present.
5050
*/
51-
private var mockMakerInlineEnabled: Boolean? = null
51+
internal var mockMakerInlineEnabled: Boolean? = null
5252

53-
private fun mockMakerInlineEnabled(jClass: Class<out Any>): Boolean {
53+
internal fun mockMakerInlineEnabled(jClass: Class<out Any>): Boolean {
5454
return mockMakerInlineEnabled ?:
5555
jClass.getResource("mockito-extensions/org.mockito.plugins.MockMaker")?.let {
5656
mockMakerInlineEnabled = File(it.file).readLines().filter { it == "mock-maker-inline" }.isNotEmpty()

mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import com.nhaarman.mockito_kotlin.*
33
import org.junit.Test
44
import java.util.*
55

6-
class ArgumentCaptorTest {
6+
class ArgumentCaptorTest : BaseTest() {
77

88
@Test
99
fun argumentCaptor_withSingleValue() {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import com.nhaarman.mockito_kotlin.mockMakerInlineEnabled
2+
import org.junit.After
3+
import org.junit.Before
4+
5+
abstract class BaseTest {
6+
7+
@Before
8+
open fun setup() {
9+
mockMakerInlineEnabled = false
10+
}
11+
12+
@After
13+
open fun tearDown() {
14+
mockMakerInlineEnabled = null
15+
}
16+
}

mockito-kotlin/src/test/kotlin/CreateInstanceTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.junit.Test
3131
import java.util.*
3232
import kotlin.reflect.KClass
3333

34-
class CreateInstanceTest {
34+
class CreateInstanceTest : BaseTest() {
3535

3636
@Test
3737
fun byte() {
@@ -555,8 +555,8 @@ class CreateInstanceTest {
555555
}
556556

557557
enum class MyEnum { VALUE, ANOTHER_VALUE }
558+
}
558559

559-
sealed class MySealedClass {
560-
class MySealedClassMember : MySealedClass()
561-
}
560+
sealed class MySealedClass {
561+
class MySealedClassMember : MySealedClass()
562562
}

mockito-kotlin/src/test/kotlin/EqTest.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.junit.Before
3131
import org.junit.Test
3232
import org.mockito.Mockito
3333

34-
class EqTest {
34+
class EqTest : BaseTest() {
3535

3636
private val interfaceInstance: MyInterface = MyClass()
3737
private val openClassInstance: MyClass = MyClass()
@@ -40,13 +40,17 @@ class EqTest {
4040
private lateinit var doAnswer: Open
4141

4242
@Before
43-
fun setup() {
43+
override fun setup() {
44+
super.setup()
45+
4446
/* Create a proper Mockito state */
4547
doAnswer = Mockito.doAnswer { }.`when`(mock())
4648
}
4749

4850
@After
49-
fun tearDown() {
51+
override fun tearDown() {
52+
super.tearDown()
53+
5054
/* Close `any` Mockito state */
5155
doAnswer.go(0)
5256
}

mockito-kotlin/src/test/kotlin/MockTest.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ import com.nhaarman.mockito_kotlin.mock
2828
import com.nhaarman.mockito_kotlin.whenever
2929
import org.junit.Test
3030
import org.mockito.Mockito.RETURNS_DEEP_STUBS
31-
import org.mockito.exceptions.base.MockitoException
3231
import java.util.*
3332

34-
class MockTest {
33+
class MockTest : BaseTest() {
3534

3635
private lateinit var propertyInterfaceVariable: MyInterface
3736
private lateinit var propertyClassVariable: MyClass
@@ -80,11 +79,6 @@ class MockTest {
8079
expect(instance).toNotBeNull()
8180
}
8281

83-
@Test(expected = MockitoException::class)
84-
fun closedClass() {
85-
mock<ClosedClass>()
86-
}
87-
8882
@Test
8983
fun deepStubs() {
9084
val cal: Calendar = mock(RETURNS_DEEP_STUBS)
@@ -94,6 +88,5 @@ class MockTest {
9488

9589
private interface MyInterface
9690
private open class MyClass
97-
private class ClosedClass
9891
}
9992

mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.nhaarman.mockito_kotlin.*
2828
import org.junit.After
2929
import org.junit.Test
3030

31-
class MockitoKotlinTest {
31+
class MockitoKotlinTest : BaseTest() {
3232

3333
@After
3434
fun teardown() {

mockito-kotlin/src/test/kotlin/MockitoTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
import com.nhaarman.expect.expect
23
import com.nhaarman.expect.expectErrorWithMessage
34
import com.nhaarman.expect.fail
45
import com.nhaarman.mockito_kotlin.*
6+
import org.junit.Assume.assumeTrue
57
import org.junit.Test
68
import org.mockito.exceptions.base.MockitoAssertionError
79
import java.io.IOException
@@ -31,7 +33,7 @@ import java.io.IOException
3133
* THE SOFTWARE.
3234
*/
3335

34-
class MockitoTest {
36+
class MockitoTest : BaseTest() {
3537

3638
@Test
3739
fun anyString() {
@@ -100,6 +102,8 @@ class MockitoTest {
100102
/** https://github.com/nhaarman/mockito-kotlin/issues/27 */
101103
@Test
102104
fun anyThrowableWithSingleThrowableConstructor() {
105+
assumeTrue(mockMakerInlineEnabled(javaClass))
106+
103107
mock<Methods>().apply {
104108
throwableClass(ThrowableClass(IOException()))
105109
verify(this).throwableClass(any())

mockito-kotlin/src/test/kotlin/SpyTest.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ import org.mockito.Mockito
3131
import org.mockito.exceptions.base.MockitoException
3232
import java.util.*
3333

34-
class SpyTest {
34+
class SpyTest : BaseTest() {
3535

3636
private val interfaceInstance: MyInterface = MyClass()
3737
private val openClassInstance: MyClass = MyClass()
3838
private val closedClassInstance: ClosedClass = ClosedClass()
3939

4040
@After
41-
fun a() {
41+
override fun tearDown() {
42+
super.tearDown()
4243
Mockito.validateMockitoUsage()
4344
}
4445

45-
4646
@Test
4747
fun spyInterfaceInstance() {
4848
/* When */
@@ -61,12 +61,6 @@ class SpyTest {
6161
expect(result).toNotBeNull()
6262
}
6363

64-
@Test(expected = MockitoException::class)
65-
fun spyClosedClassInstance() {
66-
/* When */
67-
spy(closedClassInstance)
68-
}
69-
7064
@Test
7165
fun doReturnWithSpy() {
7266
val date = spy(Date())

mockito-kotlin/src/testInlineMockito/kotlin/CreateInstanceInlineTest.kt renamed to mockito-kotlin/src/test/kotlin/inline/UsingMockMakerInlineTest.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import com.nhaarman.expect.expect
2626
import com.nhaarman.expect.expectErrorWithMessage
2727
import com.nhaarman.mockito_kotlin.*
28+
import org.junit.Assume.assumeTrue
29+
import org.junit.Before
2830
import org.junit.Test
2931
import java.io.IOException
3032
import java.math.BigInteger
3133

32-
class CreateInstanceInlineTest {
34+
class UsingMockMakerInlineTest {
3335

3436
class ClassToBeMocked {
3537

@@ -41,6 +43,12 @@ class CreateInstanceInlineTest {
4143
}
4244
}
4345

46+
@Before
47+
fun setup() {
48+
mockMakerInlineEnabled = null
49+
assumeTrue(mockMakerInlineEnabled(javaClass))
50+
}
51+
4452
@Test
4553
fun mockClosedClass() {
4654
/* When */

mockito-kotlin/src/testInlineMockito/resources/mockito-extensions/org.mockito.plugins.MockMaker

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)