import com.pholser.junit.quickcheck.runner.JUnitQuickcheck; import com.pholser.junit.quickcheck.Property; class Crypto { byte[] encrypt(byte[] plaintext, Key key) { // ... } byte[] decrypt(byte[] ciphertext, Key key) { // ... } } @RunWith(JUnitQuickcheck.class) public class SymmetricKeyCryptographyProperties { @Property public void decryptReversesEncrypt(String plaintext, Key key) throws Exception { Crypto crypto = new Crypto(); byte[] ciphertext = crypto.encrypt(plaintext.getBytes("US-ASCII"), key); assertEquals( plaintext, new String(crypto.decrypt(ciphertext, key))); } }
Note: The example above presumes the existence and availability of generators for classes String and Key. Find out more about generators in Basic Types and Generating Values of Other Types.
junit-quickcheck honors the usual JUnit machinery: @Before, @After, @Rule, @BeforeClass, @AfterClass, @ClassRule. It will also run zero-arg public void methods annotated with @Test.