Reputation: 2103
Is it possible to create multiple transactions programmaticaly inside a ledger in a contract test in Corda? When I remove the forEach loop and copy and paste the code for each transaction (changing the output state to match what I wanna test) everything works as expected. But if I try to refactor it and generate transactions programmaticaly inside forEach loop as in the code example, my test fails.
@Test
fun simpleTest() {
ledger {
listOf(...).forEach {
transaction {
command(participants, Commands.Command())
input(Contract.ID, inputState)
output(Contract.ID, outputState)
failsWith("message")
}
}
}
}
Upvotes: 0
Views: 163
Reputation: 2548
In response to your reply to my comment; I think you should try using tweak
.
In the example below you'll see how my transaction has the same command and output but I can tweak it and try different inputs:
@Test
@DisplayName("Testing different inputs.")
public void testDifferentInputs() {
CustomState goodInput = new CustomState();
CustomState badInput1 = new CustomState();
CustomState badInput2 = new CustomState();
CustomState output = new CustomState();
ledger(ledgerServices, l -> {
l.transaction(tx -> {
// Same output and command.
tx.output("mypackage.CustomState", output);
tx.command(Collections.singletonList(myNode.getPublicKey()), new CustomStateContract.Commands.Create());
// Tweak allows to tweak the transaction, in this case we're using bad input #1 and the transaction should fail.
tx.tweak(tw -> {
tw.input("mypackage.CustomState", badInput1);
return tw.failsWith("Bad Input State.");
});
// Tweak allows to tweak the transaction, in this case we're using bad input #2 and the transaction should fail.
tx.tweak(tw -> {
tw.input("mypackage.CustomState", badInput2);
return tw.failsWith("Bad Input State.");
});
// After the tweak we are using the good input and the transaction should pass.
tx.input("mypackage.CustomState", goodInput);
return tx.verifies();
});
return Unit.INSTANCE;
});
}
More examples here: https://docs.corda.net/tutorial-test-dsl.html
Upvotes: 1