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

Easy way to make a mock with settings #148

Closed
wafisher opened this issue Jan 16, 2017 · 8 comments
Closed

Easy way to make a mock with settings #148

wafisher opened this issue Jan 16, 2017 · 8 comments
Assignees
Milestone

Comments

@wafisher
Copy link

wafisher commented Jan 16, 2017

Is there an easy way to include MockSettings (in particular, RETURNS_DEEP_STUBS) using this syntax? It seems like you can either use this or this but not both.

Thanks!

@nhaarman
Copy link
Collaborator

Can you give an example of how you would use it in Java?

@wafisher
Copy link
Author

Sure, here it is in Kotlin. Mostly it's that RETURNS_DEEP_STUBS bit that I can't easily put in now.

val trip = Mockito.mock(Trip::class.java, RETURNS_DEEP_STUBS)
Mockito.`when`(trip.someFn().nestedFn()).thenReturn(returnMe)

@nhaarman
Copy link
Collaborator

Ah, so if I understand correctly you'd want some syntax like the following?

val trip = mock<Trip>(RETURNS_DEEP_STUBS) {
  on { someFn().nestedFn() } doReturn returnMe
}

@wafisher
Copy link
Author

Exactly. Is that reasonable?

@nhaarman
Copy link
Collaborator

Sounds valid. Perhaps something can be done with a parameter with a default value. I'll have a look later on!

@wafisher wafisher changed the title Easy way to make a mock with settings Easy way to make a mock with settings Jan 18, 2017
@nhaarman
Copy link
Collaborator

The MockSettings object is essentially a Builder. With that in mind, we can create the following function to replace the current withSettings():

fun withSettings(
      extraInterfaces: Array<KClass<Any>>? = null,
      name: String? = null,
      spiedInstance: Any? = null,
      defaultAnswer: Answer<Any>? = null,
      serializable: Boolean = false,
      serializableMode: SerializableMode? = null,
      verboseLogging: Boolean = false,
      invocationListeners: Array<InvocationListener>? = null,
      stubOnly: Boolean = false,
      @Incubating useConstructor: Boolean = false,
      @Incubating outerInstance: Any? = null
): MockSettings

This will not break current code as they will all use the default values.
Using this, we can do the following:

-inline fun <reified T : Any> mock(): T
+inline fun <reified T : Any> mock(
+      extraInterfaces: Array<KClass<Any>>? = null,
+      name: String? = null,
+      spiedInstance: Any? = null,
+      defaultAnswer: Answer<Any>? = null,
+      serializable: Boolean = false,
+      serializableMode: SerializableMode? = null,
+      verboseLogging: Boolean = false,
+      invocationListeners: Array<InvocationListener>? = null,
+      stubOnly: Boolean = false,
+      @Incubating useConstructor: Boolean = false,
+      @Incubating outerInstance: Any? = null
+): T 

Usage would be intuitive:

val myMock = mock<MyClass>(
  name = "Custom name",
  verboseLogging = true
)

The same can be done for the stubbing variant:

val myMock = mock<MyClass>(
  name = "Custom name",
  verboseLogging = true
) {
  on { doSomething() } doReturn true
}

If this is introduced, we can deprecate the other mock functions (mock(Answer), mock(MockSettings) and mock(String)).

What do you think?

@nhaarman nhaarman mentioned this issue Jan 19, 2017
@nhaarman nhaarman self-assigned this Jan 19, 2017
@wafisher
Copy link
Author

That looks awesome. Thanks for the quick turnaround!

@nhaarman
Copy link
Collaborator

Great! I'll do some field tests tomorrow and draft a release in the coming week probably.

@nhaarman nhaarman added this to the 1.2.0 milestone Jan 21, 2017
@nhaarman nhaarman mentioned this issue Jan 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants