Test a domain behavior in Vernac
The 0.6.0 development branch runs domain scenarios alongside Java tests. The Basic and
Spring Boot wizard projects include a scenario under src/test/vernac. In a generated
project, run ./mvnw test or mvnw.cmd test on Windows.
The Spring Boot scenario creates an open task, saves its generated ID and calls the same domain behaviors used by the application. Each scenario gets its own fixtures.
namespace org.example.tasks;
suite TaskLifecycle {
scenario followsStatusTransitions {
given task = Task(TaskTitle("Try Vernac"), TaskStatus.OPEN);
given originalId = task.id;
expect originalId is present;
expect task.taskStatus equals TaskStatus.OPEN;
when task.complete();
expect task.id equals originalId;
expect task.taskStatus equals TaskStatus.DONE;
when task.reopen();
expect task.taskStatus equals TaskStatus.OPEN;
}
}
givenbinds a fixture or captures a value for later assertions. Aggregate and entity constructors call the generatedcreatefactory. Value constructors callof.wheninvokes a public behavior on a previously bound fixture. Usewhen result = task.readStatus();to bind the result of a behavior that returns a value.expectcompares two values of the same type or checks presence. A dotted fixture path reads its fields. Capture a field before a mutation when you need to compare its previous value.expect TaskTitle(" ") is invalid;requires the generated constructor to throw a domain validation exception.- A scenario can use types in its namespace and import types from other namespaces. It supports enum members, strings, integers and booleans as inputs. Test declarations are visible only during test compilation.
IDs and long values in 0.7.0
given run = RunId("00000000-0000-4000-8000-000000000001");
given anotherRun = RunId();
given cost = MicroEur(3000000000L);
given limit = 2500000000L;
expect cost.amount equals 3000000000L;
expect (cost amount > limit);
An ID call without arguments creates an ID. A call with one UUID string uses the
generated parser factory. Pass the ID to an aggregate or entity constructor when
it needs a reference field. The aggregate’s own identity is still generated by
create. Plain whole-number literals are int; use the suffix L for a
long literal. Bind long constants with given before using them in a
parenthesized predicate. Signed integers, longs and decimals work in direct
scenario expressions.
Change the expectation after complete() to TaskStatus.OPEN and rerun Maven. JUnit reports both enum
values and the assertion’s .vernac source location. Change it to a string and the Vernac compiler
instead reports the type mismatch before JUnit starts. Restore the expected value afterwards.
For an existing project, add a separate execution of the vernac-maven-plugin goal test-compile
and a test-scoped org.junit.jupiter:junit-jupiter dependency. The goal runs in
generate-test-sources. Surefire writes the reports to target/surefire-reports.
The generated bridge has one JUnit method per scenario and uses the namespace, suite and
scenario name as its display ID.
Exercise application entry points and rendered pages
A suite can name a test-scoped Java configuration with using. The runner opens
and closes a Spring context for each case. A configuration can bind a TenantId
bean to the case scope and supply the application’s actor resolver and permission
evaluator. when id = invoke CreateTask(TaskTitle("Draft")); calls the generated
usecase through Spring. Configure transaction management for guarded operations and
event emission. when invoke DeleteDocument(id) is ACCESS_DENIED; checks the exact
authorization outcome. is invalid checks a domain validation failure.
namespace org.example.tasks;
suite TaskActions profile mvc using org.example.tasks.TaskScenarioFixtures {
scenario displaysReopenAfterCompletion {
when id = invoke CreateTask(TaskTitle("Draft"));
when request TaskWeb.complete(id = id) csrf;
when request TaskWeb.board();
expect status 200;
expect action TaskWeb.reopen(id = id) is present;
expect action TaskWeb.complete(id = id) is absent;
}
}
The mvc profile uses a test-scoped Spring MockMvc bean. A request resolves the
declared endpoint method, route and named inputs. Add csrf to submit Spring
Security’s test token. expect body contains matches raw HTML source. Action
assertions parse the rendered HTML with test-scoped Jsoup and match the form method
and expanded path, including the row ID.
In 0.7.0, assert a redirect with expect status 303; and
expect header "Location" equals "/items/00000000-0000-4000-8000-000000000001";
after the POST. The expected header value may also be a bound String. With a known
ID fixture, assert the typed route directly using
expect redirect to TaskWeb.detail(id = id);. Keep the separate status assertion.
expect emitted DocumentDeleted with documentId equals id count 1; counts
matching payloads received by a synchronous test handler. Omit the field condition
to count all emissions of the declared event type. The handler runs inside the
transaction. Read persisted state through a later usecase to verify its committed
effects. expect calls to DocumentStore.delete count 0; observes calls to the
Spring port bean while delegating to the configured test implementation.
Start with existing Spring beans and assert business failures
With using, explicitly bind a context bean with given workflow = bean WorkflowId;.
Add named "blockedWorkflow" when several beans share the type. A test scenario can
pass the bean value to a usecase like any other typed fixture. Bind TenantId explicitly
when you need to refer to the tenant already supplied by the configuration.
Use when invoke ActivateWorkflow(workflow, MicroEur(3000000000L)) is error "WORKFLOW_GOAL_UNREACHABLE";
to check the complete message of an IllegalStateException or IllegalArgumentException.
For native rejection codes use when ticket.approve() rejects AlreadyApproved; or
when invoke ApproveTicket(id) rejects AlreadyApproved;. See the
executable workflow example.
Select a profile
The default run includes domain and mvc. Pass -Dvernac.testProfile=domain,
mvc or postgres to select one scenario profile. Java tests follow Maven’s
usual selection. -Dtest=VernacScenariosTest selects the generated bridge.
A selected profile with zero matching cases fails generation. A setup failure
fails the selected case and reports its underlying cause.
The Document Workspace example includes a disposable PostgreSQL fixture for a committed deletion and event, a refused command and rollback after an injected listener failure. The release plan lists the acceptance matrix.
Run scenarios by identity
Pass a comma-separated list of fully qualified scenario IDs to the Vernac Maven plugin. A scenario ID consists of its namespace, suite name and scenario name. This command selects a single case:
mvn -pl examples/document-workspace-scenarios -am -Dvernac.testCases=example.documents.ReaderView.hiddenActionAndDirectPostAreDenied -Dtest=VernacScenariosTest -Dsurefire.failIfNoSpecifiedTests=false test
The compiler validates the scenario sources and emits JUnit methods for the selected cases. The selection is restricted to the active Vernac test profiles. An unknown ID or an ID outside those profiles fails test generation.