You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
java.lang.AssertionError: expected:<22> but was:<7>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:120)
at org.junit.Assert.assertEquals(Assert.java:146)
...
Mean lights is not getting mocked.
Please help in fixing this issue.
Thanks in advance.
The text was updated successfully, but these errors were encountered:
To get your test passing, instead of using every { car.lights } returns 20, you could directly set car.lights to be 20 (car.lights = 20). You could accomplish the same in the setUp function with the following:
@Before
fun setUp() {
car = spyk(Car()) {
lights = 20
}
}
To address the issue, I took a look into this a little bit. I found that if an explicit getter were added to lights, then the test would pass. Example:
class Car {
var lights: Int = 5
get() = field
fun getAllLights(): Int {
return lights + 2
}
}
IntelliJ identifies this as a redundant getter, but having that getter changes the Bytecode for getAllLights(). Instead of using GETFIELD, it uses INVOKEVIRTUAL.
Generated Bytecode for getAllLights() when lights lacks an explicit getter:
public final getAllLights()I
L0
LINENUMBER 5 L0
ALOAD 0
GETFIELD Car.lights : I
ICONST_2
IADD
IRETURN
Generated Bytecode for getAllLights() when lights has an explicit getter:
public final getAllLights()I
L0
LINENUMBER 6 L0
ALOAD 0
INVOKEVIRTUAL Car.getLights ()I
ICONST_2
IADD
IRETURN
L1
I believe every { car.lights } returns 20 does not work because the getter is being mocked, but since by default the getAllLights method directly accesses the field, the mocked getter is not used.
In terms of fixing it, I'm not really sure, but I wanted to post my findings in case it could help someone else.
Hi All,
I have a class
And have testing class
This test is failing with error
Mean
lights
is not getting mocked.Please help in fixing this issue.
Thanks in advance.
The text was updated successfully, but these errors were encountered: