My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
SimpleUnitTestwithMock  
Simple Mocking with jSupervisor.
Featured
Updated Aug 18, 2010 by panwei...@gmail.com

The source for this test (or example) is available in SimpleUnitTestwithMock.java

This class has two member objects with the following interfaces:

1. iApp myApp
2. iAuthentication myAuthentication ;

The test scope has summarized as: jSupervisor --> myApp --> myAuthentication, i.e. jSupervisor invokes myApp and myApp invokes myAuthentication. The object myApp is being tested and the object myAuthentication will be mocked.

This class is structured similar to SimpleUnitTest and has three test methods, namely

  • testAuthentication_successful()
  • testAuthentication_unsuccessful()
  • testAuthentication_successful_verifier()

testAuthentication_successful. This is the basic mocking scenario.

1. myApp.start() ; 
2. spec.from(myApp) ; 
3. spec.expects(true) ; 
4. myAuthentication.authenticate("Mr Foo","bar");  

Line 1 describes jSupervisor invoking myApp.start(). Line 2 changes the focus of control from jSupervisor to myApp. Line 3 indicates that myApp in the subsequent call will expect a return value "true" in Line 4. Line 4 has myApp who has the focus of control now invoking myAuthentication.authenticate() with given parameters.

Having described the scenario, preparation of the test objects is effected by attaching appropriate implementations.

5. MyApp targetApp = new MyApp() ; 
6. supervisor.attach(myApp, targetApp) ; 
7. targetApp.setAuthentication(myAuthentication) ; 

Line 5 and 6 instantiates the target object to be tested and attach it to myApp. This target object is the wired to the proxy myAuthentication in Line 7. Now, we are all set to play the scenario.

 8. supervisor.setEcho(true); 
 9. ExecutionState state = supervisor.play() ; 
10. assertEquals(state, ExecutionState.COMPLETED);  

During scenario execution, the system checks that the value returned by the mock is true as per Line 3 above. Since there is no errors the jSupervisor plays the scenario to completion and the test is complete.

testAuthentication_unsuccessful. I now demonstrate the case of an invalid scenario with myApp passing wrong values to the mock object. The scenario is modified slightly from the above as follows:

1. myApp.start() ; 
2. spec.from(myApp) ; 
3. spec.expects(true) ; 
4. myAuthentication.authenticate("Mr Foo","barn");  

Line 4 here has the parameters ("Mr Foo","barn") instead of ("Mr Foo","bar") as expected by the mock myAuthentication. So, the mock (through jSupervisor) will detect an invalid call.

 8. supervisor.setEcho(true); 
 9. ExecutionState state = supervisor.play() ; 
10. assertEquals(state, ExecutionState.PAUSED);  

Since there is an invalid value, jSupervisor will not complete the scenario and instead pause the execution.

11. Invocation invc = supervisor.currentInvocation(); 
12. System.err.println("Failed at : "+invc.getString()); 
13. System.err.println(invc.getExecution().getString(" * "));  

Printing the execution log (i.e. invc.getExecution()) shows where the execution stops and the reasons why.

testAuthentication_successful_verifier. This case shows the relaxation of test criteria. The mock object is set to accept any values.

1. myApp.start() ; 
2. spec.from(myApp) ; 
3. spec.expects(true) ; 
4. myAuthentication.authenticate("Mrs Foo","bars"); 
5. spec.expecting(Value.Any,Value.Any) ;  

Line 5 tells jSupervisor that the mock accepts any value for both parameters. As such, this scenario will play out completely.

Powered by Google Project Hosting