question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easy way to make a mock with settings

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
nhaarmancommented, Jan 19, 2017

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

1reaction
nhaarmancommented, Jan 19, 2017

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overview of Mockito MockSettings | Baeldung
As we can see, our standard set of settings for our mock objects is very simple. We configure the default answer for our...
Read more >
MockSettings (Mockito 3.11.1 API) - Javadoc.io
Firstly, to make it easy to add another mock setting when the demand comes. Secondly, to enable combining together different mock settings without...
Read more >
A Cleaner Way to Create Mocks in .NET | by Martin Rybak
I'm a stickler for code that is short, concise, and easy to read. ... If you are setting up multiple mocks in a...
Read more >
Easy way to make a mock with settings · Issue #148 - GitHub
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 ......
Read more >
Mockito mock examples - DigitalOcean
We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found