Unit testing with mock libs

701 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
701
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit testing with mock libs

  1. 1. Unit testing with PowerMock Open source project:http ://code.google.com/p/powermock/
  2. 2. PowerMock featuresMocking static methodsMocking final methods or classesMocking private methodsMock construction of new objectsPartial MockingReplay and verify allMock PoliciesTest listeners
  3. 3. Add to maven project<properties> <powermock.version>1.4.12</powermock.version></properties><dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-easymock</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency></dependencies>
  4. 4. Add to ant projectCopy jars to the test folder:cglib-nodep-2.2.2.jareasymock-3.1.jarjavassist-3.16.1-GA.jarpowermock-easymock-1.4.12-full.jarobjenesis-1.2.jarAdd jars to the classpath for junit task:<path id="test.classpath"><path refid="plugin.classpath" /><fileset dir="${app.server.lib.portal.dir}" includes="commons-io.jar" /><fileset dir="${project.dir}/lib" includes="junit.jar" /><fileset dir="${project.dir}/lib/test" includes="*.jar" /><pathelement location="test-classes" /></path>
  5. 5. Class under test examplepublic class ClassUnderTest { private MyService myService; public String getMaxAmountForTemplate() { String template = TemplateUtil.getTemplate("1"); BigDecimal maxAmount = myService.getMaxAmount(); return TemplateUtil.applyTemplate(template, maxAmount); }}interface MyService { BigDecimal getMaxAmount();}class TemplateUtil { public static String getTemplate(String templateId) { return "ID"; } public static String applyTemplate(String template, Object ... params) { return template + params.toString(); }}
  6. 6. Unit test implementationimport java.math.BigDecimal;public class ClassUnderTestTest { private ClassUnderTest classUnderTest; @org.junit.Test public void getMaxAmountForTemplate() { classUnderTest = new ClassUnderTest(); org.junit.Assert.assertEquals("Should be ID100", "ID100", classUnderTest.getMaxAmountForTemplate()); }}
  7. 7. Unit test implementation with mocks@PrepareForTest({TemplateUtil.class })@RunWith(PowerMockRunner.class)public class ClassUnderTestTest { private ClassUnderTest classUnderTest; @org.junit.Test public void getMaxAmountForTemplate() throws Exception { classUnderTest = new ClassUnderTest(); PowerMock.mockStatic(TemplateUtil.class); EasyMock.expect(TemplateUtil.getTemplate(EasyMock.eq("1"))).andReturn("ID"); EasyMock.expect(TemplateUtil.applyTemplate(EasyMock.eq("ID"), (BigDecimal) EasyMock.anyObject())).andReturn("IDMOCK"); MyService myService = EasyMock.createMock(MyService.class); Field field = classUnderTest.getClass().getDeclaredField("myService"); field.setAccessible(true); field.set(classUnderTest, myService); EasyMock.expect(myService.getMaxAmount()).andReturn(new BigDecimal("100")); PowerMock.replayAll(); EasyMock.replay(myService); org.junit.Assert.assertEquals("Should be IDMOCK", "IDMOCK", classUnderTest.getMaxAmountForTemplate()); EasyMock.verify(myService); PowerMock.verifyAll(); }}
  8. 8. Use capture with EasyMock public void getMaxAmountForTemplate() throws Exception { classUnderTest = new ClassUnderTest(); PowerMock.mockStatic(TemplateUtil.class); EasyMock.expect(TemplateUtil.getTemplate(EasyMock.eq("1"))).andReturn("ID"); Capture<BigDecimal> capturedMaxAmount = new Capture<BigDecimal>(); EasyMock.expect(TemplateUtil.applyTemplate(EasyMock.eq("ID"), EasyMock.capture(capturedMaxAmount))).andReturn("IDMOCK"); MyService myService = EasyMock.createMock(MyService.class);... EasyMock.replay(myService); org.junit.Assert.assertEquals("Should be IDMOCK", "IDMOCK", classUnderTest.getMaxAmountForTemplate()); org.junit.Assert.assertEquals("100", capturedMaxAmount.getValue().toString() ); EasyMock.verify(myService); PowerMock.verifyAll(); }
  9. 9. Code emulation in mock objects@org.junit.Testpublic void getMaxAmountForTemplate() throws Exception { classUnderTest = new ClassUnderTest(); PowerMock.mockStatic(TemplateUtil.class); EasyMock.expect(TemplateUtil.getTemplate(EasyMock.eq("1"))).andReturn("ID"); EasyMock.expect(TemplateUtil.applyTemplate(EasyMock.eq("ID"), (BigDecimal) EasyMock.anyObject())).andAnswer(new IAnswer<String>() { public String answer() throws Throwable { return EasyMock.getCurrentArguments()[0].toString() + EasyMock.getCurrentArguments()[1].toString(); } }); MyService myService = EasyMock.createMock(MyService.class); Field field = classUnderTest.getClass().getDeclaredField("myService"); field.setAccessible(true); field.set(classUnderTest, myService); EasyMock.expect(myService.getMaxAmount()).andReturn(new BigDecimal("100")); PowerMock.replayAll(); EasyMock.replay(myService); org.junit.Assert.assertEquals("Should be ID100", "ID100", classUnderTest.getMaxAmountForTemplate());
  10. 10. Skip static initialization for classes under testpublic class TemplateUtil { static { initDatabase(); } public static String getTemplate(String templateId) { return "ID"; } public static String applyTemplate(String template, Object ... params) { return template + params[0].toString(); }}@SuppressStaticInitializationFor("TemplateUtil")@RunWith(PowerMockRunner.class)public class ClassUnderTestTest { private ClassUnderTest classUnderTest;
  11. 11. F.I.R.S.T. properties of unit tests Fast Many hundreds or thousands per second Isolates Failure reasons became obvious Repeatable Run repeatedly in any order, any time Self-validating No manual validation required Timely Written before the code
  12. 12. Internet resourceshttp ://stackoverflow.com/search?q=powermockhttp ://code.google.com/p/powermock/w/list

×