-1

I want to check if it returns with same value by using EasyMock's andReturn method. Unfortunately, I come across with "java.lang.IllegalStateException: missing behavior definition for the preceding method call:" when I use EasyMock. I guess it is not possible to test by EasyMock when I try expect method. You will understand question better in the code.

Regards Alper

Menu menu = EasyMock.createMock(Menu.class)
menu.setName("name");
        EasyMock.expect(XmlParseUtility.createLinesToParse(menu).toString()).andReturn(angularLines.toString());

Error Message :

 java.lang.IllegalStateException: missing behavior definition for the preceding     method call:
   Menu.getName()
   Usage is: expect(a.foo()).andXXX()
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Tonyukuk
  • 5,745
  • 7
  • 35
  • 63
  • That's not what the error is about. EasyMock encountered a method call that you didn't tell it about. Because of how it works, it generates an exception when that happens. There are two solutions: 1. tell the library every method that it should expect to be called. 2. Switch to mocking library that allows unexpected method calls. – M. Prokhorov Feb 28 '17 at 12:04
  • If you mean " EasyMock.expect(menu.getName()).equals("name"); ", I already did it. What do you mean by switching to mocking library Prokhorov, ? Can you give me an example ? – Tonyukuk Feb 28 '17 at 12:13
  • I meant different mocking library is all. For example, Mockito or Spock. EasyMock is a "strict" mocking library, and it'll have you providing all possible methods being called on the mocks. This might dilute your test with effectively meaningless stuff that you did not intend to test (although actual effect will change case by case, sometimes you want this strictness). Mockito is a "lenient" mocking library, it will ignore unexpected method calls. – M. Prokhorov Feb 28 '17 at 12:55
  • thank you Prokhorov. As you told me, I will be using Mockito – Tonyukuk Mar 01 '17 at 05:23

2 Answers2

0

I'm not sure what you wanted to do. To comment one of the comment, EasyMock isn't strict. It is whatever you want.

If you want a Mockito style mock, you will use niceMock.

Then, about your code. I feel like you want to record a call to setName. And then want to make sure the XmlParseUtility.createLinesToParse is working as expected. If I'm right, you want this code:

Menu menu = EasyMock.createNiceMock(Menu.class); // unrecorded methods will return null
menu.setName("name"); // recording a call to setName
replay(menu); // done with recording, going in replaying
assertEquals(angularLines.toString(), XmlParseUtility.createLinesToParse(menu).toString());
verify(menu); // if you want to make sure setName was called
Henri
  • 5,551
  • 1
  • 22
  • 29
0

If you are actually trying to stub the static XmlParseUtility.createLinesToParse() method call, then the PowerMock library is what you are looking for.

// instantiate angularLines and menu

PowerMock.mockStatic(XmlParseUtility.class);
EasyMock.expect(XmlParseUtility.createLinesToParse(menu)).andReturn(angularLines);
PowerMock.replay(XmlParseUtility.class);

// invoke test subject

PowerMock.verify(XmlParseUtility.class);

PowerMock also requires that the test be run using their runner and that the class containing the static method be "prepared". For more information, check out their documentation

callmepills
  • 672
  • 9
  • 13