Scrivio Interview library
CodingIntermediate7 min read · updated 12 November 2026

What is property-based testing? Describe a property-based test with jqwik for a function that serialises and deserialises an Order.

In property-based testing we state a rule that must hold for every valid input, and the tool generates hundreds of inputs to try to break it. With jqwik we write an @Property method with @ForAll parameters, and for an Order serialiser the natural rule is a round trip: deserialising the serialised order gives back an equal order. When the rule fails, jqwik shrinks the failing order to the smallest example that still fails, and custom arbitraries make sure every generated order is a valid one.

Why this question is asked

Example-based tests check the cases we thought of. Bugs, however, tend to hide in the cases we did not think of: an empty list, a name with a quote in it, a price with three decimal places. Property-based testing is a way of asking the computer to think of those cases for us. Interviewers ask about it to see whether we can move from examples to rules, and a serialiser is the classic first property. In this answer, we will recap the idea, write the Order and its generators, write the round-trip property, see what a failure and shrinking look like, and fix the bug it finds.

1From Examples to Properties

A normal unit test picks an input and an expected output: serialise this order with two lines and expect this JSON. That is an example. A property is a statement that is true for all inputs of a kind, for example: for any order, reading back what we wrote gives the same order. We do not say what the JSON looks like; we only say what must hold.

A property-based testing tool then generates inputs, by default jqwik tries 1,000 of them per property, and checks the statement for each. It deliberately includes edge cases such as empty strings, zero, the minimum and maximum of a range, and empty lists, because those are where code tends to break.

jqwik is a test engine for the JUnit Platform, the same platform that runs JUnit Jupiter tests. That means it runs from Maven, Gradle and the IDE next to our normal tests, but it uses its own annotations: @Property instead of @Test, and @ForAll on each parameter that jqwik should generate.

2The Order and Its Serialiser

Here is the code under test: a small record and a serialiser that uses Jackson.

Java
record OrderLine(String sku, int quantity, BigDecimal unitPrice) {}
record Order(String id, String customer, List<OrderLine> lines) {}
class OrderCodec {
private final ObjectMapper mapper = new ObjectMapper();
String serialize(Order o) throws Exception { return mapper.writeValueAsString(o); }
Order deserialize(String json) throws Exception { return mapper.readValue(json, Order.class); }
}

Records give us equals() based on all components, which is exactly what a round-trip property needs: two orders are equal when every field is equal. If our Order were a class without equals(), the first step would be to compare fields explicitly.

3Generating Valid Orders With Custom Arbitraries

jqwik can generate strings and integers on its own, but it does not know what a valid order looks like. An arbitrary is jqwik's name for a generator of values of one type. We build an arbitrary for Order out of arbitraries for its parts, and we put it in a method marked @Provide.

Java
@Provide
Arbitrary<Order> orders() {
Arbitrary<String> ids = Arbitraries.strings().alpha().numeric().ofLength(8);
Arbitrary<String> names = Arbitraries.strings().all().ofMinLength(1).ofMaxLength(40); // any characters
Arbitrary<OrderLine> lines = Combinators.combine(
Arbitraries.strings().alpha().ofLength(6), // sku
Arbitraries.integers().between(1, 99), // quantity
Arbitraries.bigDecimals().between(new BigDecimal("0.01"), new BigDecimal("9999.99")).ofScale(2)
).as(OrderLine::new);
return Combinators.combine(ids, names, lines.list().ofMinSize(1).ofMaxSize(5))
.as(Order::new);
}

Notice that the generators encode the business rules: quantities from 1 to 99, prices with exactly two decimal places, at least one line. This matters. If we generated negative quantities, the property might fail for inputs that the real system never accepts, and we would waste time on false alarms. Combinators.combine joins several arbitraries and as() builds the object from the generated parts.

The customer name deliberately allows any characters, including quotes, backslashes and non-Latin letters, because those are the inputs that break hand-written serialisers.

4Writing the Round-Trip Property

Now the property itself. It reads almost like the sentence we started with.

Java
class OrderCodecProperties {
OrderCodec codec = new OrderCodec();
@Property(tries = 500)
void roundTripKeepsTheOrder(@ForAll("orders") Order order) throws Exception {
String json = codec.serialize(order);
assertEquals(order, codec.deserialize(json));
}
}
// timestamp = ..., OrderCodecProperties:roundTripKeepsTheOrder =
// tries = 500 | checks = 500 | ... | result: SUCCEEDED

@ForAll("orders") tells jqwik to use our @Provide method, matched by its name. tries = 500 lowers the default of 1,000 to keep the test fast. jqwik prints a short report after each property with the number of tries and the seed it used, so a failing run can be repeated exactly.

5When the Property Fails: Shrinking

Suppose someone later replaces Jackson with a hand-written serialiser that formats prices with toString() and parses them with new BigDecimal(), but writes the customer name without escaping quotes. A generated order with a name such as Ann "Q" Lee and four lines would fail. A raw failing input like that is hard to read, so jqwik shrinks it: it tries smaller and simpler versions of the input, keeps any version that still fails, and reports the smallest one it finds.

Java
// org.opentest4j.AssertionFailedError: ... unexpected character
// Shrunk Sample (12 steps)
// order: Order[id=AAAAAAAA, customer=", lines=[OrderLine[sku=AAAAAA, quantity=1, unitPrice=0.01]]]
// Original Sample
// order: Order[id=x7Q2pL0a, customer=Ann "Q" Lee, lines=[... 4 lines ...]]
Step throughA failing property and its shrinking
jqwikPropertyOrderCodec
try generated order #137
serialize, then deserialize
different order: assertion fails
try a smaller order: fewer lines
still fails
try a shorter name: one quote
still fails: keep it
report the shrunk sample and the seed

8 messages in order. Play them, or step through them one at a time.

Shrinking keeps simplifying the input while the property still fails.

The shrunk sample tells the story at a glance: a customer name made of a single double quote is enough to break the serialiser. That is the value of shrinking. Instead of debugging a random order with four lines, we get a minimal case that points straight at the cause. We fix the escaping, and we can also add the shrunk case as a normal example test so it stays documented.

Round-trip properties catch other real bugs too: BigDecimal values that come back as 10.5 instead of 10.50 when a serialiser drops trailing zeros (and records then compare unequal, because BigDecimal.equals() compares scale), timestamps that lose nanoseconds, and empty lists that come back as null.

6Follow-up questions

How many tries are enough?

The default of 1,000 is a good start for fast code like a serialiser, since it runs in well under a second. For slower properties we lower it and rely on edge cases, which jqwik mixes in early. More tries find rarer bugs, but the biggest gain usually comes from good generators rather than huge try counts.

How do you reproduce a failure that happened in CI?

jqwik reports the seed of the failing run, and by default it reruns the previous failing sample first on the next run, using a database file it keeps in the project. We can also pin a seed with @Property(seed = "...") while debugging. Then we add the shrunk example as a regular test.

Does property-based testing replace example tests?

No. Examples document behaviour that matters to humans, such as a specific JSON format required by a partner. Properties cover the wide space of inputs we would never write by hand. A good suite keeps a few clear examples and adds properties for rules like round trips, invariants and comparisons with a simpler reference implementation.

7Conclusion

In this answer, we moved from example tests to a property: for any valid Order, deserialising the serialised order gives back an equal order. We wrote custom arbitraries that encode what a valid order is, a @Property method with an @ForAll parameter that jqwik runs on the JUnit Platform, and saw how jqwik shrinks a failing order to a minimal case such as a name made of one quote. Round-trip properties are cheap to write and regularly find escaping, precision and null-handling bugs.

What to say in the interviewAbout 90 seconds aloud

Open with

Property-based testing means that instead of picking example inputs, I state a rule that must hold for every valid input, and the tool generates many inputs to try to break it.

Then, if they want more

For a serialiser, the obvious rule is a round trip: for any order, deserialise of serialise of the order equals the order.

With jqwik, which is a test engine on the JUnit Platform, I write an @Property method that takes an Order parameter annotated with @ForAll. jqwik cannot invent valid orders by itself, so I write a @Provide method that returns an Arbitrary of Order, built with Combinators.combine from arbitraries for the id, the customer name, and a list of one to five order lines with quantities from 1 to 99 and prices with two decimal places.

The generators encode the business rules, so failures are real. The name allows any characters, because quotes and non-Latin letters are what break serialisers. By default jqwik runs a thousand tries and mixes in edge cases like empty strings.

When it finds a failure, it shrinks the input to the smallest order that still fails, for example a customer name that is just a double quote, which points straight at missing escaping. It also prints the seed, so I can rerun it, and I usually keep the shrunk case as a plain example test.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is property-based testing? Describe a property-based test with jqwik for a function...": https://scrivio-interview-library.netlify.app/java/test-strategy/d4b727e53e/