Created
November 14, 2013 20:13
-
-
Save charyorde/7473558 to your computer and use it in GitHub Desktop.
Mock objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unit testing with Mock objects using Mockito | |
-------------------------------------------- | |
2 concepts | |
- mock (create object using mocks) | |
- stub (modify a created object with stub). useful methods when stubbing are: | |
- when | |
- doReturn | |
How mock object works | |
--------------------- | |
- First, we define a Mock object that implements the same interface as an object on which the SUT (domain object) depends | |
- Then during the test, we configure the Mock object with the values with which it should respond to the SUT | |
- Before exercising the SUT, we install the Mock object so that the SUT uses it instead of the real implementation | |
- When called during SUT execution, the Mock object compares the actual arguments received wth the expected arguments using Equality Assertions | |
Implementation steps (notes) for mock object | |
-------------------------------------------- | |
- Test constructs Mock Object | |
- Test configures Mock Object | |
- Test installs Mock Object into domain object (SUT) - Make Mock Object a dependency e.g by annotating it or instantiating the mock object | |
- SUT calls Mock Object (happens in the background) | |
- Mock object does assertions | |
- Test calls final verification method | |
Mock example without a framework | |
-------------------------------- | |
public void testRemoveFlight_Mock() throws Exception { | |
// fixture setup | |
FlightDto expectedFlightDto = createAnonRegFlight(); | |
// mock configuration | |
ConfigurableMockAuditLog mockLog = new ConfigurableMockAuditLog(); | |
mockLog.setExpectedLogMessage( | |
helper.getTodaysDateWithoutTime(), | |
Helper.TEST_USER_NAME, | |
Helper.REMOVE_FLIGHT_ACTION_CODE, | |
expectedFlightDto.getFlightNumber()); | |
mockLog.setExpectedNumberCalls(1); | |
// mock installation | |
FlightManagementFacade facade = new FlightManagementFacadeImpl(); | |
facade.setAuditLog(mockLog); | |
// exercise | |
facade.removeFlight(expectedFlightDto.getFlightNumber()); | |
// verify | |
assertFalse("flight still exists after being removed", | |
facade.flightExists(expectedFlightDto.getFlightNumber())); | |
mockLog.verify(); | |
} | |
Mock example using a framework | |
------------------------------ | |
public void testRemoveFlight_JMock() throws Exception { | |
// fixture setup | |
FlightDto expectedFlightDto = createAnonRegFlight(); | |
FlightManagementFacade = new FlightManagementFacadeImpl(); | |
// mock configuration | |
Mock mockLog = mock(AuditLog.class); | |
moclLog.expects(once()).method("logMessage) | |
.with(eq(helper.getTodaysDateWithoutTime()), | |
eq(Helper.TEST_USER_NAME), | |
eq(Helper.REMOVE_FLIGHT_ACTION_CODE), | |
eq(expectedFlightDto.getFlightNumber())); | |
// mock installation | |
facade.setAuditLog(AuditLog) mocklog.proxy()) | |
// exercise | |
facade.removeFlight(expectedFlightDto.getFlightNumber()); | |
// verify | |
assertFalse("flight still exists after being removed", | |
facade.flightExists(expectedFlightDto.getFlightNumber())); | |
// verify() method called automatically by JMock | |
} | |
Understanding the execution process of a Mock object | |
---------------------------------------------------- | |
- When the SUT calls the methods of the Mock Object, the methods compare the method call (method name plus arguments) with the expectations. If the method call is unexpeced or the arguments are incorrect, the assertion fails the test immediately | |
- If the call is expected but came out of sequence (a reason to use Inorder), a strict Mock Object fails the test | |
- If the method call has any outgoing parameters or return values, the Mock Object needs to return or update something to allow the SUT to continue executing the test scenario. | |
Unit test and mock objects in Jive | |
---------------------------------- | |
- JiveCommunityTest or one of it's subclasses must be extended by your test class in order to be able to run a test in Jive successfully | |
- When running test with JiveCommunityTest, it cannot be run with (RunWith) powermock since it's already running with @Refreshable (com.jivesoftware.util.junit.runners.Refreshable) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment