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
mockkStatic(Instant::class) failed with stdObjectAnswer than equals/hashCode/toString with Spring Boot 3.0.x and JDK 17.0.x #1075
Comments
We're experiencing the same problem when mocking Clock |
In our case it seemed to be an issue in combination with logback (version 1.4.5). When a As a workaround, we simply reduced the log level for the <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Disable mockk logging to prevent stdObjectAnswer MockKException when mocking time -->
<logger name="io.mockk.impl.instantiation.AbstractMockFactory" level="error"/>
<logger name="io.mockk.impl.recording.states.AnsweringState" level="error"/>
</configuration> |
@e-wag thanks, but it's not working for me. |
I got this error from a HasMap, I fixed by manually returning a map val theMapToFix = mapOf<…>()
val mockObj = mockk<…> {
every { getterOfTheMap } returns theMapToFix
} |
Unfortunately I am observing the same issue and the workaround from @e-wag unfortunately does not fix it |
I reproduced this with normal mocking, not mockstatic. And what I realized is that we are not supposed to use the mocked objects as if they were real objects. |
This isn't Spring related. I got the same bug today when I tried to use mockkStatic for Instant::class in a non-spring environment. (specifics are work related) I'm going to see if I can figure out what's going on and open a PR to fix it |
@SamuelRuwe any update on this? Currently, in spring boot apps we have to fall back to an old version because of some incompatibility with Spring Boot, it would be great if we can move forward and keep the version aligned with SB reference |
Hey @artemptushkin, I got side tracked with other things. I'll try to get some time this weekend to look into it. If you're using Spring one thing you could do is use indirection by creating a bean that provides Instant to your services that you can also mock out for testing. It's been a bit since I've done Spring, but something like the below example may fit your use case @Bean
public InstantProvider instantProviderCreator() {
return new InstantProviderImpl();
}
interface InstantProvider {
Instant now();
}
class InstanceProviderImpl implements InstantProvider {
@Override
public Instant now() {
return Instant.now();
}
}
// in the tests
class MockInstanceProvider implements InstantProvider {
@Override
public Instant now() {
return Instant.EPOCH; (or any other arbitrary date you want for testing)
}
} |
@SamuelRuwe as a workaround it's okay but I here care mostly about being up-to-date with the latest versions w/o a need to write a code. We want to be on the latest just to be aligned |
Please remove sections wisely
Below information is actually needed to make all the process of fixing faster.
Choose main points. Don't blindly follow this as a set of rules.
Don't waste much time. Usually, the main thing is to have a good reproducible minimal code.
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
Please describe the behavior you are expecting
This piece of code should work correctly
Current Behavior
What is the current behavior?
Failed with error
No other calls allowed in stdObjectAnswer than equals/hashCode/toString
Failure Information (for bugs)
Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
2.7.10
3.0.5
No other calls allowed in stdObjectAnswer than equals/hashCode/toString
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
1.13.4
macOS Ventura 13.3
1.7.22
openjdk 17.0.6 2023-01-17
jupiter 5.9.2
Stack trace
Minimal reproducible code (the gist of this issue)
The text was updated successfully, but these errors were encountered: