Skip to main content

Type Refinement

Refine the generated data-table Param classes with enums (or any custom type) to turn data drift into a compile error.

Source: examples/common-use-cases/example-8 on GitHub.

What this demonstrates

  • Move the generated XxxParam class up into the marker class
  • Change one of its fields from String to a custom enum
  • The generator detects the existing class and uses it instead of regenerating one
  • Invalid values in the feature file become compilation errors
  • Compile-time safety for data table content — a key SpecBinder differentiator

Step 1 — Compile once, copy the generated class

On first compilation, the generator emits ProductsParam with String/inferred types:

// Generated (before refinement)
public static class ProductsParam {
private final String name;
private final Integer qty;
private final Double unitPrice;
private final String category; // ← String by default
// ...
}

Step 2 — Refine in the marker class

Move ProductsParam into the marker class. Change category to an enum:

public enum Category { electronics, grocery, sports }

public static class ProductsParam {
// ...
private final Category category; // ← now an enum
// ...
}

Step 3 — Compile-time safety

The generator now uses your refined ProductsParam. Generated call sites reference the enum constants directly:

new ProductsParam("Wireless Headphones", 1, 59.99, Category.electronics)
new ProductsParam("Coffee Beans 1kg", 3, 12.50, Category.grocery)

Step 4 — Catch drift

If someone adds a row with an unknown category:

| Yoga Mat | 1 | 25.00 | fitness |

The generator emits Category.fitnesscompilation error. The spec/data mismatch is caught before tests run.

You can apply the same trick to refine String → any value type that has a valueOf(String) method or a matching constant — not just enums.

Run it

cd examples/common-use-cases/example-8
mvn test