T
T
Tsuzukeru2020-12-04 13:55:30
Java
Tsuzukeru, 2020-12-04 13:55:30

Why does the default implementation of the stub method fire?

Let's say there is a class A, with a method doSomething(arg:Int):Boolean, which returns a Boolean and can throw an exception. I don’t give its code because it’s a fictional example.
I want to make a stub of this method. I do it in the following way.

class ATest{
    @Test
    fun `test method`(){
        val a = mock(A::class.java)
        val param = 5
        `when`(a.doSomething(param)).thenReturn(false)
        assertFalse(a.doSomething(param))
    }
}


When executing the code, NPE crashes. Why is this happening? After all, Mock is an instance of a class, with an empty implementation of methods. By default my method should return false. I additionally program this (although I could not do it) by creating a stub `when`(a.doSomething(param)).thenReturn(false). Where does the exception come from? Is it empty if we create a Mock of this class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tsudzukeru, 2020-12-10
@Tsuzukeru

you need to make the stub open method and then everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question