• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Unit testing with mock libs
 

Unit testing with mock libs

on

  • 794 views

 

Statistics

Views

Total Views
794
Views on SlideShare
794
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Unit testing with mock libs Unit testing with mock libs Presentation Transcript

    • Unit testing with PowerMock Open source project:http ://code.google.com/p/powermock/
    • PowerMock featuresMocking static methodsMocking final methods or classesMocking private methodsMock construction of new objectsPartial MockingReplay and verify allMock PoliciesTest listeners
    • 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>
    • 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>
    • 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(); }}
    • 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()); }}
    • 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(); }}
    • 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(); }
    • 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());
    • 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;
    • 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
    • Internet resourceshttp ://stackoverflow.com/search?q=powermockhttp ://code.google.com/p/powermock/w/list