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.

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


My previous code was:


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


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

1ReloadableResourceBundleMessageSource mockMessage =
2PowerMock.createMock(ReloadableResourceBundleMessageSource .class);
3        expect(mockMessage.getMessage("text",
4null, 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.

Sunday, July 14, 2013

non-parseable settings in maven

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

1[ERROR] Error executing Maven.
2[ERROR] 1 problem was encountered while building the effective settings
3[FATAL] Non-parseable settings C:\Program Files
4(x86)\Apache\apache-maven-3.0.5\conf\settings.xml: entity reference
5names can not start with character '<' (position: START_TAG seen
6...<password>myPassword<... @12:33)  @ C:\Program Files
7(x86)\Apache\apache-maven-3.0.5\conf\settings.xml, line 12, column 33
8</password>
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.