Test Anything Protocol

Testing with Java

Testing using tap4j


In order to install tap4j you have to download a jar from https://tupilabs.com/tap4j/tap4j or if you are a Maven user you can add a dependency in your pom.xml, as shown below.


Differently than in others implementations, tap4j uses an Object Oriented approach. Instead of calling methods like ok(), not_ok(), is() and isnt(), you construct objects such as a TAP Producer to dump TAP and a TAP Consumer to load TAP. A TestSet object is used to encapsulate all the TAP Elements.

Generating TAP using a TAP Producer

The TAP Producers in tap4j are created using a TapProducerFactory. After a TAP Producer is created we can call the dump method passing a TestSet to have the result TAP.

TapProducer tapProducer = TapProducerFactory.makeTap13Producer();
testSet testSet = new testSet();

// Creating a Plan with 2 test Result's
Plan plan = new Plan( 2 );

// Creating a test Result with a status ok
testResult tr1 = new testResult( StatusValues.OK, 1 );

// Creating a test Result with status not_ok
testResult tr2 = new testResult( StatusValues.NOT_OK, 2 );

// Retrieving the TAP content and printing it to the default output
String tapStream = tapProducer.dump( testSet );
System.out.println( tapStream );

The code above will generate the following TAP output:

ok 1
not ok 2

Reading TAP using a TAP Consumer

If you understood about how tap4j works, reading a TAP Stream will be quite easy for you. You just have to create a TAP Consumer using guess what? Yeah, a TapConsumerFactory. The method of the TAP Consumer to load TAP is load(). This method will return a TestSet. Let’s read the output from the previous section.

TapConsumer tapConsumer = TapConsumerFactory.makeTap13Consumer();
String tapStream = "1..2\n" +
		"ok 1\n" +
		"not ok 2";
testSet testSet = tapConsumer.load(tapStream);




The code above will generate the following output:

not ok