Mocking data classes by defaulting properties
See original GitHub issueHi, 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:
- Created 3 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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