Shyam Singh
Shyam Singh

Reputation: 41

Corda - transaction.fails() not working while running Test

I am very new to Corda. transaction.fails() not working while running Test

---- Code ---

@Override
public void verify(LedgerTransaction tx) throws IllegalArgumentException {
    Command command = tx.getCommand(0);
    private final TestIdentity alice = new TestIdentity(new CordaX500Name("Alice", "", "GB"));                       
    private final TestIdentity bob = new TestIdentity(new CordaX500Name("Bob", "", "GB"));                           
    private MockServices ledgerServices = new MockServices(new TestIdentity(new CordaX500Name("TestId", "", "GB"))); 
    private TokenState tokenState = new TokenState(alice.getParty(), bob.getParty(), 1);                             

    if (tx.getInputStates().size() != 0) {
        System.out.println(" -- Checking Input Size -- ");
        throw new IllegalArgumentException(" Transaction Must have No Inputs ");
    }
}

--- in Test Case --

@Test                                                                    
public void tokenContractRequiresZeroInputsInTheTransaction() {                
    transaction(ledgerServices, tx -> {                                       
        //Has an input, will fail.                                            
        tx.input(TokenContract.ID, tokenState);                              
        tx.output(TokenContract.ID, tokenState);                             
        tx.command(alice.getPublicKey(), new TokenContract.Commands.Issue());
        tx.fails();                                                          
        return null;                                                         
});                                                                             

Upvotes: 0

Views: 58

Answers (1)

Joel
Joel

Reputation: 23140

This test will pass, as expected.

The call to tx.fails() means that given the transaction as it currently stands, calling the verify method should throw an exception for at least one of the transaction's contracts. If it doesn't, tx.fails() will throw an exception, causing the test to fail.

In your case, calling the verify method of your TokenContract will throw an exception because the transaction has an input, causing the contract to throw an exception. tx.fails() will therefore not throw an exception, and your test will pass.

P.S. You shouldn't use test classes such as TestIdentity and MockServices directly inside verify.

Upvotes: 1

Related Questions