Home > Software engineering >  JMockit test a manual transaction with transaction template
JMockit test a manual transaction with transaction template

Time:08-22

I have a code which I want to test with JMockit. Can you please help how to test the inside functionalities with this ?

Code to test : transactionTemplate is defined as :

@Resource
private TransactionTemplate transactionTemplate;

void methodToTestEvent(Type fileType, String exceptionCategory) {
        transactionTemplate.execute(new TransactionCallback<Object>() {

            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                String accountName;
                String documentNumber ;
               // More Methods

return null;
            }
        });
    }

The JMockit which i did to test this method is not going inside the transactionTemplate.execute and returning me invocation error which I was trying to test in Verifications()

Can you please help how can i invoke the transactionTemplate, such that, I can test inside contents of methodToTestEvent via JMockit in replay model . Source : http://jmockit.github.io/tutorial.html

CodePudding user response:

Use Delegates.

you should do something like:

new Expectations()  {{
 transactionTemplate.execute((TransactionCallback)any);
 result = new Delegate() {
     Object execute(TransactionCallback callback) {
      callback.doInTransaction((TransactionStatus)any)
 }
 }
 }}
  • Related