Skip to content
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

when does not throw MissingMethodInvocationException #3270

Open
ac-rdn opened this issue Feb 15, 2024 · 1 comment
Open

when does not throw MissingMethodInvocationException #3270

ac-rdn opened this issue Feb 15, 2024 · 1 comment

Comments

@ac-rdn
Copy link

ac-rdn commented Feb 15, 2024

Given these two classes

class Greeting { String sayHi() { return "Hi!"; } }
class Person {
    String greet(Greeting greeting) { return sayHi(greeting); }
    String sayHi(Greeting greeting) { return greeting.sayHi(); /* LINE1 */ }
}

And the following test class

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class PersonTest {
    @Mock
    private Greeting greeting;

    @Test
    void greet() {
        Person person = new Person();
        when(person.sayHi(greeting)).thenReturn("Hello!"); // LINE2

        String actual = person.greet(greeting);

        assertThat(actual).isEqualTo("Hello!");
    }
}

I was astonished to see that this test runs successfully, because I would have expected a MissingMethodInvocationException being thrown on LINE2 as I am trying to stub a method call on a non-mock object. If you change LINE1 to return "Hello!"; the expected MissingMethodInvocationException is thrown. Why is this exception not thrown in the SSCCE? 😕

@yanapeycheva
Copy link

Hello,
can I contribute on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants