testingjunitmockitoshiro

The method expect(boolean) is undefined for the type in Shiro test


I'm doing my first test in Java, and I have a Shiro Security... I follow the tutorial (https://shiro.apache.org/testing.html) but says:

(this example uses EasyMock, but Mockito works equally as well):

Subject subjectUnderTest = createNiceMock(Subject.class);
expect(subjectUnderTest.isAuthenticated()).andReturn(true);

Because I use Mockito I implement with

Subject mockSubject = mock(Subject.class);
expect(subjectUnderTest.isAuthenticated()).andReturn(true);

But when I do it have this error

The method expect(boolean) is undefined for the type AdminControllerTest

And don't give me the posibility to import it. I don't know if expect is especific of EasyMock and if yes what I have to use in Mockito.

I search here and see more person doing it and always recomend use this expect

How to mock a shirosession?


Solution

  • If we look at this code example ...

    Subject mockSubject = mock(Subject.class);
    expect(subjectUnderTest.isAuthenticated()).andReturn(true);
    

    We can see that ...

    1. You are using mockito syntax to do the mocking.
    2. You are using easyMock syntax to configure the mock. It is not even in the dependency list, so this method is not found.

    The solution is to use mockito syntax to configure the mock.

    Subject mockSubject = mock(Subject.class);
    when(mockSubject.isAuthenticated()).thenReturn(true);
    

    This will make everything work as expected and your Subject will return true, when the isAuthenticated() method is called.

    If you want to up your mockito game, try this resource, which comes with working github code examples.