Wednesday, July 17, 2013

java.lang.illegalstateexception: missing behavior definition for the preceding method call

I have the following exception while mocking a final method with PowerMock.

java.lang.illegalStateException: missing behavior definition for the preceding method call


My previous code was:


 ReloadableResourceBundleMessageSource mockMessage =
createMock(ReloadableResourceBundleMessageSource.class);
        expect(mockMessage.getMessage("text",
null, Locale.ENGLISH)).andReturn("error_message");
 replay(mockMessage);


Then I realized that createMock() and replay() belongs to EasyMock not PowerMock.
I explicitly did these calls:

 ReloadableResourceBundleMessageSource mockMessage =
PowerMock.createMock(ReloadableResourceBundleMessageSource .class);
        expect(mockMessage.getMessage("text",
null, Locale.ENGLISH)).andReturn("error_message");
        PowerMock.replay(mockMessage);


But that's not enough. For PowerMock to work, you'll also need to add few annotations before the class definition.


@RunWith(PowerMockRunner.class)
@PrepareForTest(ReloadableResourceBundleMessageSource.class)

Notice that @PrepareForTest can also be defined just before the test method.
After these fixes my test begins to work.

Sunday, July 14, 2013

non-parseable settings in maven

I saw a very odd error while building our project with maven.

 
[ERROR] Error executing Maven.
[ERROR] 1 problem was encountered while building the effective settings
[FATAL] Non-parseable settings C:\Program Files
(x86)\Apache\apache-maven-3.0.5\conf\settings.xml: entity reference
names can not start with character '<' (position: START_TAG seen
...myPassword<... @12:33)  @ C:\Program Files
(x86)\Apache\apache-maven-3.0.5\conf\settings.xml, line 12, column 33
Nothing in the project changed so it shouldn't spit me an error. Then I saw the error line and remembered that I changed my corporate password and put it in the settings.xml because our corporate proxy need it for working. So if you see a similar error, keep in mind that this is because and invalid character in the xml file. My password contained an ampersand and thus I had this issue.