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
SimpleUnitTest  
Simple Unit Testing with jSupervisor.
Featured
Updated Aug 18, 2010 by panwei...@gmail.com

The source for this test (or example) is available at SimpleUnitTest.java

This class has three test methods, namely:

  1. testAuthentication_successful()
  2. testAuthentication_unsuccessful()
  3. testAuthentication_successful_verifier()

testAuthentication_successful. This is the most basic of tests. It is used to test a target class. This is expressed by the two lines of code.

1. spec.expects(true) ; 
2. myAuthentication.authenticate("Mr Foo","bar");

Line 1 says that the invocation in Line 2 expects a true as return value. Line 2 specifies the invocation with the jSupervisor (as personified by the spec object) invoking myAuthentication.authenticate with parameters "Mr Foo" and "bar".

The class under test is MyAuthentication. You test this class by attaching it to the appropriate proxy.

3. MyAuthentication target = new MyAuthentication() ; 
4. supervisor.attach(myAuthentication,target) ; 
5. target.setAuthenticationServer(new MyAuthenticationServer()) ; 

The specified scenario is executed by getting the supervisor to play().

6. ExecutionState state = supervisor.play() ;
7. assertEquals(state, ExecutionState.COMPLETED); 

Sincce this is a valid scenario, it will play until the scenario is completed, i.e. all specified invocations are executed.

testAuthentication_unsuccessful. This test case is very much the same as above, except that the expected output value is incorrect.

1. spec.expects(true) ; 
2. myAuthentication.authenticate("Mrs Foo","bar"); 

Here, I pass in an invalid input which cannot be authenticated. The system will return a false. Butt the expected return value is true (see Line 1). So, the scenario cannot be executed fully. It will fail at when returning.

6. ExecutionState state = supervisor.play() ; 
7. assertEquals(state, ExecutionState.PAUSED); 

So, the correct excution will result in the invocation being paused instead of completed.

testAuthentication_successful_verifier. In the above cases, jSupervisor matches the return value exactly. In many cases, you might want to relax the verification (or assertion). This is demonstrated by this test case.

1. spec.expects(true,Value.Any()) ; 
2. myAuthentication.authenticate("Mrs Foo","bar"); 

Notice that Line 1 has a second parameter Value.Any. This tells jSupervisor to accept any return value, i.e. no checking is conducted. There are other verifications, namely:

  • Any
  • Exactly (this is the default verification)
  • IsNull
  • IsNotNull

Powered by Google Project Hosting