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 properties/fields

See original GitHub issue

Unfortunately the following doesn’t work:

open class ClassUnderTest ( val property:String )
val mock = mock<ClassUnderTest>()
whenever( mock.property ).thenReturn("value")
assert( mock.property == "value" )

because:

when() requires an argument which has to be ‘a method call on a mock’

I think I understand the root cause of this but it still would be extremely helpful. I think while accessing variables directly is often considered a code smell in Java it is quite common for Kotlin.

Do you see any chance to be able to change that behaviour?

Issue Analytics

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

github_iconTop GitHub Comments

11reactions
nhaarmancommented, Sep 8, 2016

The property val needs to be open as well:

open class ClassUnderTest (open val property:String )

The relevant part in the error message:

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
4reactions
rachitmishracommented, Jan 21, 2019

Nope, Then I started using stubs for the above issue. Also a lot of other testing problems got resolved by using mockk lib.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Properties | JustMock Documentation - Telerik
Mocking properties is similar to mocking methods, but there are a few cases that need special attention like mocking indexers and particular set...
Read more >
Can I mock a class whose fields don't have getter property
How to mock a class who don't have getter properties?. I am new in JUnit and mockito can anyone help me with this?...
Read more >
Mocking Public Properties - Docs - Mockery
Mockery allows us to mock properties in several ways. One way is that we can set a public property and its value on...
Read more >
Mocking Objects with Restricted Access Members - Wright
In this post, I describe how to create class instances and access/set members from other assemblies when their access levels are set to ......
Read more >
Creating Mocks and Spies in Mockito with Code Examples
How to Inject Mocked Dependencies for the Class/Object under Test? ... dependencies which are injected via Constructor or Property fields.
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