Showing posts with label junit. Show all posts
Showing posts with label junit. Show all posts

Thursday, 8 August 2013

PowerMock: Sample usage of expectNew and expectStrictNew...

Depencencies


  • 'org.powermock:powermock-api-easymock:1.5.1'
  • 'org.powermock:powermock-module-junit4:1.5.1'
  • 'org.easymock:easymock:3.1'

PowerMockDemo

public class PowerMockDemo {
 
  public void someMethod(String someArgument){
  
  ClassA a = new ClassA();
  ClassB b = new ClassB();
  ClassC c = new ClassC();

  c.setFirst(a);
  c.setSecond(b);
  c.execute(someArgument);
 }
}

PowerMockDemoTest

Below is the unit test to test the PowerMockDemo.java.

import static org.powermock.api.easymock.PowerMock.*;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(PowerMockDemo.class)
public class PowerMockDemoTest {
 
 private PowerMockDemo toTest;
 
 @Test
 public void testExcuteMethod() throws Exception {
  
  toTest = new PowerMockDemo();
 
  ClassA mockA = createMock(ClassA.class);
  expectNew(ClassA.class).andReturn(mockA);
  
  ClassB mockB = createMock(ClassB.class);
  expectNew(ClassB.class).andReturn(mockB);
  
  ClassC mockC = createStrictMock(ClassC.class);
  expectNew(ClassC.class).andReturn(mockC);
  
  mockC.setFirst(mockA);
  expectLastCall().once();
  
  mockC.setSecond(mockB);
  expectLastCall().once();
  
  mockC.execute(EasyMock.anyObject(String.class));
  expectLastCall().once();
  
  replayAll();
  
  // run the test code
  toTest.someMethod("Hello PowerMock");
  
  verifyAll();
 }
}

In the unit test above we asserted several things when the PowerMockDemo#someMethod(String) is invoked.

PowerMock.createMock() - is used to instantiate a new mock object.

PowerMock.expectNew() - is used to specify that a new a new instance is expected and when it is invoked the specified mock object is returned.

PowerMock.createStrictMock() - is also used to create a new mock object. The only difference is that, when compared to createMock(), the order of method invocations on the mock object is asserted.

References:

Wednesday, 31 July 2013

Mockito: Using ArgumentCaptor to capture argument values for further assertions.

Sometimes there are situations where I need to assert the arguments that are passed into a service, but that arguments are instantiated within a method that you want to test and in which case it cannot be asserted using a plain JUnit test.

With Mockito, I can use the ArgumentCaptor to captures values and assert them.

To explain this better, consider the following simple scenario in the CarSelector.java class.
In this class, I want to verify that every time the method buyRedFerrari() is invoked, which subsequently invoked the service (CarBuilderService.buildCar) method to build a car, it passes the expected instance of BuyingCriteria.
In this case, when the CarBuilderService.buildCar is called, it is passed with the argument that the car has to be RED and the model is a FERRARI.

Usage examples:


References:
Mockito
ArgumentCaptor API