Java Stub Harness

Component Testing for Java

Component Testing for Java supports the following verification methods for stubbed classes:

Stub Mechanism Overview

The Component Testing for Java test harness provides a stub logging mechanism. The purpose of this mechanism is to check that calls to a stubbed object are achieved in a correct order.

For each test, the enter, exit and fail methods of the stubbed objects are logged by the object TestSynchroStub.

You can then query the TestSynchroStub object to:

Use the test harness assert or verify primitives to add the stub sequence or failure verification results into a formal test report.

Stub Sequence Verification

Use the StubSequence object to test the stub sequence. This object can be loaded with the sequence under test. The actual comparison is performed with the corresponding method of the object TestSynchroStub.

The following example demonstrates how to verify the entry into a method

public void teststub3()

{

  NoStub another = new NoStub();

  another.call1();

  StubSequence testof = new StubSequence(this);

  verifyLogMessage("verify one enter method");

  testof.addEltToSequence( new StubbedOne().getClass() , "methodone", StubInfo.ENTER) ;

  verifyEquals("Test single sequence",TestSynchroStub.isSeqRespected(testof),true);

}

 

This example shows how to verify the entry into methodone of the class StubbedOne and that the method m1 of StubbedTwo has been successively entered and exited. This is part of the call stack of the methods of stubbed objects.

public void teststub4()

{

  NoStub another = new NoStub();

  another.call1();

  StubSequence testof = new StubSequence(this);

  testof = new StubSequence(this);

  testof.addEltToSequence( new StubbedOne().getClass() , "methodone",StubInfo.ENTER) ;

  testof.addEltToSequence( new StubbedTwo().getClass() , "m1",StubInfo.ENTER) ;

  testof.addEltToSequence( new StubbedTwo().getClass() , "m1",StubInfo.EXIT) ;

  verifyLogMessage("Check true for stub calls");

  verifyEquals("Test single sequence",TestSynchroStub.isSeqRespected(testof),true);

}

 

Stub Failure Detection

In the Component Testing for Java test harness, stubs can declare an error by use of the fail() method.

To check for the existence of a stub error, use the following global call type:

  verify("Test single sequence",TestSynchroStub.areStubfail(this),true);

 

The following example demonstrates the use of TestSynchroStub to test if a stub has been declared failed.

public void testStubFail()

{

  StubbedThree st = new StubbedThree();

  st.call();

  verifyLogMessage("Check fail call from stub");

  verifyEquals("Test single sequence",TestSynchroStub.areStubfail(this),true);

}

 

Related Topics

Java Test Harness