- Java 100%
| core | ||
| example | ||
| .gitignore | ||
| LICENSE | ||
| pom.xml | ||
| README.md | ||
jcontract: Contract Testing in the JVM
jcontract tries to solve a problem that is common in unit testing: Every time we stub or mock an interface, we make an assumption. When the behavior of the implementation changes, but the interface stays (syntactically) the same, the assumption stops being true, but tests on the caller side will still pass. jcontract makes sure that stubs and real implementations behave the same (to a reasonable level of confidence).
jcontract supports both sides:
- It can provide a ready-to-use stub that already behaves according to the interface's contract.
- It can verify concrete implementations of the interface against the contract.
While it is possible to just write a behavior test on the implementation side for each stubbed behavior, jcontract actually keeps them in sync, while also making sure that stubbed assumptions are the same in all consumer side tests.
Using the terminology popularized by J. B. Rainsberger:
- We call the test on the consumer side a collaboration test. It answers the question "Does the consumer use the interface correctly?".
- We call the test on the provider side a contract test. It answers the question "Does the implementation fulfill the contract promised by the interface?".
Especially when the dependency inversion principle is used (e.g., when using the ports and adapters architectural pattern), the consumer of an interface (port) defines the contract of that port. The implementing side (adapter), who may potentially even be a different person or team, can verify that they are fulfilling all of the assumptions that the consumer has made explicit in the contract.