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

MockSettings.strictness() not observed #3262

Open
5 tasks done
lesiak opened this issue Feb 5, 2024 · 2 comments
Open
5 tasks done

MockSettings.strictness() not observed #3262

lesiak opened this issue Feb 5, 2024 · 2 comments

Comments

@lesiak
Copy link

lesiak commented Feb 5, 2024

Mock created with Mockito.withSettings().strictness(Strictness.STRICT_STUBS) does not behave in the same way as @Mock(strictness = Mock.Strictness.STRICT_STUBS)

FooRepository.java

public interface FooRepository {
    boolean isFooPresent(int fooId);
}

FooService.java

public class FooService {

    private final FooRepository fooRepository;

    public FooService(FooRepository fooRepository) {
        this.fooRepository = fooRepository;
    }

    String getSomething(int fooId) {
        var found = fooRepository.isFooPresent(fooId);
        return String.valueOf(found);
    }
}

FooServiceTest.java

@ExtendWith(MockitoExtension.class)
class FooServiceTest {

    @Mock(strictness = Mock.Strictness.STRICT_STUBS)
    FooRepository fooRepositoryExtension;

    FooRepository fooRepositoryRegular = Mockito.mock(FooRepository.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));

    @Test
    void testLenUsingAnnotationBasedMock() {
        FooService fooService = new FooService(fooRepositoryExtension);
        Mockito.when(fooRepositoryExtension.isFooPresent(1)).thenReturn(true);
        fooService.getSomething(2);
        // throws PotentialStubbingProblem
    }

    @Test
    void testLenUsingRegularMock() {
        FooService fooService = new FooService(fooRepositoryRegular);
        Mockito.when(fooRepositoryRegular.isFooPresent(1)).thenReturn(true);
        fooService.getSomething(2);
        // passes
    }

}

Observed behaviour:

  • In testLenUsingAnnotationBasedMock test PotentialStubbingProblem is thrown
  • testLenUsingRegularMock passes

Expected behaviour:

  • both tests throw PotentialStubbingProblem

Quick inspection of code shows:

  • In Mockito.mock version CreationSettings.stubbingLookupListeners are empty
  • In @Mock version CreationSettings.stubbingLookupListeners contain DefaultStubbingLookupListener

Software versions

  • mockito-core: 5.7.0
  • mockito-junit-jupiter: 5.3.1
  • junit-jupiter-engine: 5.9.3

See also
https://stackoverflow.com/questions/77940311/potentialstubbingproblem-solved-by-moving-a-class-how-does-it-work

  • The mockito message in the stacktrace have useful information, but it didn't help
  • The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • Provide versions (mockito / jdk / os / any other relevant information)
  • Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • Read the contributing guide
@rootExplorr
Copy link

Hi! Is this issue still open ??

@bjmi
Copy link
Contributor

bjmi commented Apr 14, 2024

Related to #2831

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

3 participants