Skip to content

Instantly share code, notes, and snippets.

@milenkovicm
Created March 12, 2012 00:00
Show Gist options
  • Save milenkovicm/2018762 to your computer and use it in GitHub Desktop.
Save milenkovicm/2018762 to your computer and use it in GitHub Desktop.
[Junit] EasyMock - few examples, how to use it
package acme;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;
public class AnEasyMockTestClass {
@Test
public void run_input_job_test() {
/*
...
*/
// how to expect void method
this.aService.callVoidMethod(null);
EasyMock.expectLastCall();
EasyMock.replay(this.aService);
/*
...
*/
EasyMock.verify(this.serviceIncoming);
}
@Test
public void test_save(){
/*
...
*/
// how to capture a method parameter
// prepare capture object
Capture<MultiValueMap<String, String>> captureMap = new Capture<MultiValueMap<String, String>>();
// do the capture
EasyMock.expect(this.restTemplate.postForObject(EasyMock.eq(fullUrl), EasyMock.capture(captureMap), EasyMock.eq(String.class)))
.andReturn(result);
EasyMock.replay(this.restTemplate);
/*
...
*/
// use the captured object in asserts
assertEquals(A_VALUE, captureMap.getValue().get(PARAM_CLASS).get(0));
assertTrue(captureMap.getValue().get(PARAM_PROPERTY_VALUES).get(0).contains(PARAM));
EasyMock.verify(this.restTemplate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment