EasyMock: test that method in mock isn’t called

I know this question is very old but I had the same question as the OP and did some more looking around. I found the following solution: By adding .andThrow(new AssertionFailedError()).anyTimes(); at the end of your EasyMock declaration the test will fail when the mocked method is called. The reason this is better than simply … Read more

How can I mock a method in easymock that shall return one of its parameters?

Well, the easiest way would be to just use the Capture in the IAnswer implementation… when doing this inline you have to declare it final of course. MyService mock = createMock(MyService.class); final Capture<ParamAndReturnType> myCapture = new Capture<ParamAndReturnType>(); expect(mock.someMethod(capture(myCapture))).andAnswer( new IAnswer<ParamAndReturnType>() { @Override public ParamAndReturnType answer() throws Throwable { return myCapture.getValue(); } } ); replay(mock) This … Read more

EasyMock void method

You’re close. You just need to call the method on your mock before calling expectLastCall() So you expectation would look like this: userService.addUser(newUser1); EasyMock.expectLastCall(); EasyMock.replay(dbMapper); userService.addUser(newUser1); This works because the mock object is in Record mode before the call to replay(), so any calls to it will perform default behaviour (return null/do nothing) and will … Read more

How do I remove the warning from a EasyMock.anyObject(List.class) call

It’s not possible. I’ll call it a generic limitation. Sadly it’s not always possible to remove a warning for some perfectly normal usage (like using the class of a generic class). However, with EasyMock you can do the following: EasyMock.<List<MyType>> anyObject() which will do the same thing but without warning. The anyObject you used exist … Read more

java.lang.IllegalStateException: missing behavior definition for the preceding method call getMessage(“title”)

You need to call EasyMock.replay(mock) before calling the method under test. After calling the method under test you can call EasyMock.verify(mock) to verify the mock is called. Next you need to add another expect call with the “title” argument since you call it twice. Code: EasyMock.expect(mockMessageResourse.getMessage(“title”)).andReturn(“title”); EasyMock.expect(mockMessageResourse.getMessage(“ClassB.title”)).andReturn(“someTitle”); EasyMock.replay(mockMessageResourse); clientMessages = new ClientMessages(mockMessageResourse); classToTest = new … Read more

How to mock the HttpServletRequest? [duplicate]

Use some mocking framework e.g. Mockito or JMock which comes with mocking capacity of such objects. In Mockito, you can do mocking as: HttpServletRequest mockedRequest = Mockito.mock(HttpServletRequest.class); For details on Mockito, see: How do I drink it? on the Mockito site. In JMock, you can do mocking as : Mockery context = new Mockery(); HttpServletRequest … Read more

NoClassDefFoundError when using Powermock

I just solved this one now, when I added the @RunWith(PowerMockRunner.class) attribute, eclipse automatically imported: import org.powermock.modules.junit4.legacy.PowerMockRunner; All I needed to do is change it to be: import org.powermock.modules.junit4.PowerMockRunner; And now it works fine with JUnit 4.8.2. The 2nd runner is for when running with older versions of JUnit – specifically 4.3 and older.

EasyMock: How do I create a mock of a genericized class without a warning?

AFAIK, you can’t avoid the unchecked warning when a class name literal is involved, and the SuppressWarnings annotation is the only way to handle this. Note that it is good form to narrow the scope of the SuppressWarnings annotation as much as possible. You can apply this annotation to a single local variable assignment: public … Read more