Software architecture
Adapter contract test
Run the same behavioral assertion for different implementations. Executed Java 21 fixture.
Formal Syntax & Structure
java syntaxjava
check(Stock s)Working Example
Example Snippetjava
public class Main { interface Stock { int available(String sku); } static class Memory implements Stock { private final java.util.Map<String,Integer> data = java.util.Map.of("PEN",4); public int available(String sku) { return data.getOrDefault(sku,0); } } static void check(Stock s) { if (s.available("PEN") != 4 || s.available("BOX") != 0) throw new AssertionError("stock contract"); } public static void main(String[] args) { check(sku -> sku.equals("PEN") ? 4 : 0); check(new Memory()); System.out.println("two adapters accepted"); }}