Strategy design pattern in parameterized tests for Java

public class Pizza {
private final String type;
public Pizza(final String type) { this.type = type; }
public String getType() { return this.type; }
public class PizzaRequest {
private Optional<String> type = Optional.empty();
public PizzaRequest withType(final String type) {
this.type = Optional.of(type);
return this;
public Optional<String> getType() {return this.type;}
public class PizzaFactory {
static final String DEFAULT_PIZZA_TYPE = "margherita";
private String type = DEFAULT_PIZZA_TYPE;
public void setDefaultType(final String type) { this.type = type;} // "pizza business logic"
public Pizza makePizza(final PizzaRequest request) {
final String resultType = request.getType().orElse(this.type);
return new Pizza(resultType);
public void shouldCreatePizzaWithUserRequestedTypeOverFactoryOne() {
// given
final PizzaFactory pf = new PizzaFactory();
final PizzaRequest request = new PizzaRequest()
// when
final Pizza result = pf.makePizza(request);
// then
assertThat(result.getType(), equalTo("cheese"));
public void test(final PizzaTestConfiguration ptc) {
// given
final PizzaFactory pf = new PizzaFactory();
final PizzaRequest request = new PizzaRequest();
// when
final Pizza result = pf.makePizza(request);
// then
// Simple object carrying data how the factory and pizza 
// should be customized (if at all!), and expected result.
private static class PizzaTestConfiguration {
private final Optional<String> factoryType;
private final Optional<String> pizzaType;
private final String expected;
PizzaTestConfiguration(final String factoryType,
final String pizzaType,
final String expected) {
this.factoryType = Optional.ofNullable(factoryType);
this.pizzaType = Optional.ofNullable(pizzaType);
this.expected = Objects.requireNonNull(expected);
void configurePizzaFactory(final PizzaFactory pizzaFactory) {
void configureRequest(final PizzaRequest pizzaRequest) {
String expectedResultPizzaType() {
return this.expected;
private static final String CHEESE = "cheese";
private static final String MUSHROOM = "mushroom";
public static List<PizzaTestConfiguration> testParameters() { // No configuration -> we should get default pizza flavour.
PizzaTestConfiguration c1 = new PizzaTestConfiguration(
null, null, PizzaFactory.DEFAULT_PIZZA_TYPE);
// Only request configured -> we should get request's flavour.
PizzaTestConfiguration c2 = new PizzaTestConfiguration(
// Only factory configured -> we should get factory's flavour.
PizzaTestConfiguration c3 = new PizzaTestConfiguration(
// Both request & factory configured -> request wins.
PizzaTestConfiguration c4 = new PizzaTestConfiguration(
return Arrays.asList(c1, c2, c3, c4);
public void test(final PizzaTestConfiguration ptc) {
... // then
// Extracting interface out of PTC might also be a good idea.
PizzaTestConfiguration c5 = new PizzaTestConfiguration(...) {
void assertResult(final Pizza pizza) {
// custom code related to toppings, soft drinks, etc.
  • org.junit.jupiter:junit-jupiter-engine (for the main engine)
  • org.junit.jupiter:junit-jupiter-params (for parameterized test support)
  • org.hamcrest:hamcrest-library (for matchers)




Software developer, Java mostly, C & C++ sometimes (

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Digital Analytics at CXL — Blog 4

Build a linked list

5 Traits That Hamper Software Developers’ Career Growth at Some Point

Superman toy

Access Specifiers With Real World Example

Handling ASCII Character in Python

Orchestrate Your Platform · Scaleout Ninja

7 things I learned at Generate CSS 2019

30 Days of Kotlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Kotwasinski

Adam Kotwasinski

Software developer, Java mostly, C & C++ sometimes (

More from Medium

Circuit Breaker Pattern With Netflix-Hystrix: Java

Decorator design pattern

Chain of Responsibility Pattern(Behavioral Design Pattern)

Different calling forms of REST services with Feign client in Spring Cloud