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.

Mocking data classes by defaulting properties

See original GitHub issue

Hi, is there a way to mock data classes by calling their constructor with all values mocked?

I have data classes with tens of values and I have to instantiate them with some values even though I don’t need all the values for my particular test. I don’t want to specify default values in their constructors since default values are only for tests.

For example. if I have the following data class with many properties:

data class Foo(
        val a: String,
        val b: Int,
        val c: Boolean,
        val d: Long,
        // ... more properties here
)

I would like this test to work:

// call Foo constructor by mocking all arguments to defaults like empty string, 0 or false
val bar = spyk<Foo>().copy(a = "bar", c = true)
assertEquals("bar", bar.a)
assertEquals(true, bar.c)
assertEquals(bar, spyk<Foo>().copy(a = "bar", c = true))  // data class equals used

Is there a way to do something like this in mockk or would it be possible to implement it? I could look into it if you point me in the right direction.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
headsvkcommented, Oct 26, 2020

@Raibaz actually mocking a data class is not a good idea as it breaks the data class contract, copy/equals/toString doesn’t work as expected, so using such mocked class would cause issues in tested code.

1reaction
Raibazcommented, Oct 20, 2020

In that case, you can still declare a val foo: Foo = mockk().

You can then either have your mock provide default answers by passing relaxed = true, or specifying the answers you want on your own like

every { foo.a } returns "a value"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Taking Advantage of Kotlin Data Classes in Android | by mvndy
Today, I'm going to talk about the benefits of using Kotlin data classes — in particular, working with them in unit testing.
Read more >
Creating a mock object with default properties - Stack Overflow
The class I'm mocking is an abstract model, and I want to give it some $fields so it will behave like a real...
Read more >
Mock Static Classes, Methods, and Properties | Telerik JustMock
JustMock allow software developers to mock static classes and calls to static members like methods and properties, set expectations and verify results.
Read more >
How to mock final classes on Kotlin using Mockito 2 (KAD 23)
In Kotlin all classes and functions are closed by default. Luckily, Mockito 2 has removed this restriction, ... You can also mock properties...
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
A mock intended to be used as a property, or other descriptor, on a class. PropertyMock provides __get__() and __set__() methods so you...
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