0

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?

Ulises 2010
  • 478
  • 1
  • 6
  • 16
  • Please show us your import of `expect`. Do you have one? – Lesiak Nov 06 '21 at 19:07
  • Thanks @Lesiak, but this is the problem... I don't have any import and Eclipse don't suggest where I can import :-( – Ulises 2010 Nov 07 '21 at 07:58
  • If you have static import for `createNiceMock` why don't you try adding one for `expect`. They come from the same class `EasyMock`, it seems not likely that you can import one but not the other – Lesiak Nov 07 '21 at 08:32
  • Thanks again... and I'm sorry... I don't use EasyMock but Mockito (I edit the question), and if I try to import says: The import org.mockito.Mockito.expect cannot be resolved – Ulises 2010 Nov 07 '21 at 11:11
  • See https://stackoverflow.com/questions/14392148/what-is-the-mockito-equivalent-of-expect-andreturn-times – Lesiak Nov 07 '21 at 11:27
  • Thanks a lot Lesiak... thats the guide I need.... If you want write the answer and I choose it... if not, I answer the question myself to help someone need it!!!! – Ulises 2010 Nov 07 '21 at 16:00

1 Answers1

2

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.

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17