Top Banner
Junit-contracts: A Contract Testing Tool Claude N. Warren, Jr. CloudStack Collaboration Conference Europe October 8-9, 2015 Dublin, Ireland
32

Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Aug 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Junit-contracts: A Contract Testing Tool

Claude N. Warren, Jr.

CloudStack Collaboration Conference Europe

October 8-9, 2015

Dublin, Ireland

Page 2: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Who Is Claude Warren?

[email protected]●Apache Jena Project Management CommitteeMember and Committer.●https://github.com/Claudenw●Playing with java since version 0.8●Developer/Architect > 25 years experience●Currently employed by IBM (Galway, IE)●Formerly employed by Digital Enterprise Research Institute (Galway, IE), National Renewable Energy Laboratory (Golden, CO, USA)●Founding member of the Denver Area Mad Scientists Club●Winner of the first Critter Crunch (Robotics Competition)●Frustrated Musician●Author of Junit Contract test extension.

Page 3: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

What is Contract Testing

● A Java interface outlines a contract.

Page 4: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

What is Contract Testing

● A Java interface outlines a contract.

● The contract is further refined and defined in other documentation.

Page 5: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

What is Contract Testing

● A Java interface outlines a contract.

● The contract is further refined and defined in other documentation.

● Contract testing ensures that all testable facets of the contract are tested.

Page 6: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

What is Contract Testing

● A Java interface outlines a contract.

● The contract is further refined and defined in other documentation.

● Contract testing ensures that all testable facets of the contract are tested.

● A jUnit extension written by Claude Warren and found at https://github.com/Claudenw/junit-contracts

Page 7: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Why Contract Testing

● Verify that implementations are correct.

– Support can ask for proof of correctness of 2nd or 3rd party implementations.

– Internal development teams can ensure that they are correctly implementing the interface long before integration test.

● Apply DRY (Don't Repeat Yourself) principles to interface testing. One test covers all implementations.

– A class can have multiple interfaces but only one parent, so consistent testing across implementations is difficult.

Page 8: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Problem

public interface Foo { // Add an object. Implementations must log action. public void add( Object x ); Public boolean contains( Object x ); // register a logger to listen. Multiple calls ok. public void register( Logger log );}

Page 9: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Problem

public interface Foo { // Add an object. Implementations must log action. public void add( Object x ); Public boolean contains( Object x ); // register a logger to listen. Multiple calls ok. public void register( Logger log );}

Page 10: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Problem

public interface Foo { // Add an object. Implementations must log action. public void add( Object x ); Public boolean contains( Object x ); // register a logger to listen. Multiple calls ok. public void register( Logger log );}

How do you verify that all implementations adhere to the logging requirement?

Page 11: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Problem

public interface Foo { // Add an object. Implementations must log action. public void add( Object x ); Public boolean contains( Object x ); // register a logger to listen. Multiple calls ok. public void register( Logger log );}

How do you verify that all implementations adhere to the logging requirement?

How do you find all the implementations?

Page 12: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution

● Define a “producer” that provides the instance of the interface.

Page 13: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution

● Define a “producer” that provides the instance of the interface.

● Define a concrete contract test that tests the instance returned by a “producer”

Page 14: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution

● Define a “producer” that provides the instance of the interface.

● Define a concrete contract test that tests the instance returned by a “producer”

● Define a jUnit extension that locates all the contract tests, associates them with the interface they test and locates all the classes that implement that interface.

Page 15: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Producer Interface

public interface IProducer<T>() {

public T newInstance();

public void cleanUp();

};

Page 16: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution Diagram

Foo

FooImpl1

Page 17: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution Diagram

Foo Foo_CT @Contract(Foo.class)

FooImpl1

Page 18: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Solution Diagram

Foo Foo_CT @Contract(Foo.class)

FooImpl1 FooImpl1_CS@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

Page 19: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

@Contract@Contract(Foo.class)public class Foo_CT<T extends Foo> {

private IProducer<T> fooProducer;

@Contract.Inject public final void setFooContractTestProducer(IProducer<T> fooProducer) { this.fooProducer = fooProducer; }

@ContractTest public void testAdd() { TestingLogger logger = … Object testObject = … Foo foo = fooProducer.newInstance(); foo.register( logger ); foo.add( testObject ); assertTrue( logger.recordedAdd() ); assertTrue( foo.contains( testObject ) ); }

@After public void cleanup() { fooProducer.cleanUp(); }…}

Foo Foo_CT

Page 20: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

@ContractImpl(FooImpl1.class)@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)public class FooImpl1_CS {

private IProducer<FooImpl1> fooProducer;

public FooImpl1_CS() {

fooProducer = new IProducer<FooImpl1>() {

@Override public FooImpl1 newInstance() { return new FooImpl1(); }

@Override public void cleanUp() { // nothing to do }

}; }

@Contract.Inject public final IProducer<FooImpl1> getTestProducer() { return fooProducer; }}

FooImpl1 FooImpl1_CS

Page 21: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

FooImpl1 FooImpl1_CS@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl

Page 22: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo

FooImpl1 FooImpl1_CS@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.

Page 23: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.3.Find all classes annotated with Contract and which test an

ancestor interface.

Page 24: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.3.Find all classes annotated with Contract and which test an

ancestor interface.4. Instantiate each class found in step 3.

Page 25: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.3.Find all classes annotated with Contract and which test an

ancestor interface.4. Instantiate each class found in step 3.5.Create a jUnit suite comprising all ContractTest annotated methods

found in the class from step 3.

Page 26: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.3.Find all classes annotated with Contract and which test an

ancestor interface.4. Instantiate each class found in step 3.5.Create a jUnit suite comprising all ContractTest annotated methods

found in the class from step 3.6.Get the Producer object from the contract suite and insert in class

instantiated in step 4.

Page 27: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Runtime Result

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

1.Find class specified in ContractImpl2.Find all ancestors of the class that are interfaces.3.Find all classes annotated with Contract and which test an

ancestor interface.4. Instantiate each class found in step 3.5.Create a jUnit suite comprising all ContractTest annotated methods

found in the class from step 3.6.Get the Producer object from the contract suite and insert in class

instantiated in step 4.7.Execute the suite.

Page 28: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Complex Solution Diagram

Foo Foo_CT

FooImpl1 FooImpl1_CS

@Contract(Foo.class)

@RunWith(ContractSuite.class)@ContractImpl(FooImpl1.class)

Bar Bar_CT @Contract(Bar.class)

Page 29: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Who Benefits

● SPI/API Implementers – Insure full implementation.

● SPI/API Definers – Insure that other teams correctly implement contracts.

● QA Test – can easily validate that contracts are correctly implemented.

● QA Test Managers – can easily determine which contract tests need to be developed or implemented.

Page 30: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

Bits and Bobs

● There can be more than one ContractImpl for a single concrete class.

● ContactImpl has a skip property to ignore specific interface tests (e.g bar.class).

● Coverage reporting:

– Unimplemented Tests

– Untested Interfaces● Maven reporting plugin.

● Provides a Dynamic interface which triggers testing of the classes returned from methods.

Page 31: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

More Info

● https://github.com/Claudenw/junit-contracts

● Maven:<dependency> <groupId>org.xenei</groupId> <artifactId>junit-contracts</artifactId> <version>0.1.5</version></dependency>

● Simplifying Contract Testing, Dr. Dobb's Journal, May 2014. http://www.drdobbs.com/testing/simplifying-contract-testing/240167128

Page 32: Junit-contracts: A Contract Testing Tool...What is Contract Testing A Java interface outlines a contract. The contract is further refined and defined in other documentation. Contract

QA