I have the following exception while mocking a final method with PowerMock.
1 | java.lang.illegalStateException: missing behavior definition for the preceding method call |
My previous code was:
1 | ReloadableResourceBundleMessageSource mockMessage = |
2 | createMock(ReloadableResourceBundleMessageSource. class ); |
3 | expect(mockMessage.getMessage( "text" , |
4 | null , Locale.ENGLISH)).andReturn( "error_message" ); |
Then I realized that createMock() and replay() belongs to EasyMock not PowerMock.
I explicitly did these calls:
1 | ReloadableResourceBundleMessageSource mockMessage = |
2 | PowerMock.createMock(ReloadableResourceBundleMessageSource . class ); |
3 | expect(mockMessage.getMessage( "text" , |
4 | null , Locale.ENGLISH)).andReturn( "error_message" ); |
5 | PowerMock.replay(mockMessage); |
But that's not enough. For PowerMock to work, you'll also need to add few annotations before the class definition.
1 | @RunWith (PowerMockRunner. class ) |
2 | @PrepareForTest (ReloadableResourceBundleMessageSource. class ) |
Notice that @PrepareForTest can also be defined just before the test method.
After these fixes my test begins to work.