How to test anonymous methods with JUnit or Mockito?

10,342

Solution 1

Why it doesn't work

Well your problem here is that TransactionTemplate in your test is a mock. As such it has the same interface as TransactionTemplate but it does not know how to behave. You are in charge of its implementation - that's the whole point of mocks. You are explicitly calling template.execute() in your code and that is why your first verify passes. But that execute() isn't the one from Spring (or more precisely template in your test isn't an instance of Spring's TransactionTemplate, it's only a mock of it) - it's, well lets say it's "empty" as it is invoked on a mock and you did not tell the mock how invoking execute() on it should behave.

How I would fix it

In cases like this I would really discourage you from such unit tests because you are testing implementation here. What you should test, at least according to me, is the functionality meaning given certain conditions, when something happens then some result should occur. This would require changing this to an integration test (using lets say DBUnit or anything else) and asserting if you actually deleted what you were supposed to delete. I mean what do you really care about - knowing that some methods got invoked or that something you hoped for actually happened?

How you can, but IMHO shouldn't, fix it.

But if you really want to test that anonymous piece of code then I would simply extract it (the whole anonymous class) to a separate class and write a unit test just for that new class and more precisely for it's doInTransaction() method. In that case you would create it using new, setting a mock DataWarehouseMessageDao in it and simply do your verify().

Solution 2

You shouldn't change your code, it's right. In your unit test, instead of isA check, you should use ArgumentCaptor to capture passed parameter, validate that instance is type of TransactionCallback and invoke doInTransaction method on it. So you would be able to verify that dao was invoked with expected parameter (advice you could use eq matcher to verify exact value).

It's true that in this test you gonna test two things at a time, but it's only because of your implementation and I'm not saying that it's wrong. Creating new instances with some business logic always increase coupling in your code, but it doesn't mean that we shouldn't use language abilities to do that.

Share:
10,342

Related videos on Youtube

Dmitrii Borovoi
Author by

Dmitrii Borovoi

Updated on September 16, 2022

Comments

  • Dmitrii Borovoi
    Dmitrii Borovoi over 1 year

    I have simple class but with anonymous block of code. I need to cover this class with tests.

    public class CleanerTask {
    
        private final Logger log = LoggerFactory.getLogger(getClass());
        DataWarehouseMessageDao dwMessageDao;
        int cleanerDelay = 0;
        TransactionTemplate template;
    
        public CleanerTask(DataWarehouseMessageDao dwMessageDao, int cleanerDelay, TransactionTemplate template) {
            this.dwMessageDao = dwMessageDao;
            this.cleanerDelay = cleanerDelay;
            this.template = template;
        }
    
        public void clean() {
            log.info("Cleaner started");
            final Date olderThan = new Date();
            olderThan.setDate(olderThan.getDate() + cleanerDelay);
            template.execute(new TransactionCallback<Date>() {
                @Override
                public Date doInTransaction(TransactionStatus transactionStatus) {
                    dwMessageDao.deleteAllByStatusAndDate(DataWarehouseMessageStatus.SAVED.getValue(), olderThan);
                    return olderThan;
                }
            });
        }
    }
    

    And test:

    @RunWith(MockitoJUnitRunner.class)
    public class CleanerTaskTest {
    
        final static int CLEANER_DELAY = 5;
    
        @Mock
        DataWarehouseMessageDao dao;
    
        @Mock
        TransactionTemplate template;
    
        CleanerTask cleanerTask;
    
        @Before
        public void setUp() throws Exception {
            cleanerTask = new CleanerTask(dao, CLEANER_DELAY, template);
        }
    
        @Test
        public void successfulScenario() {
            try {
                cleanerTask.clean();
                verify(template, times(1)).execute(isA(TransactionCallback.class));
                //that verify was not triggered    
                //verify(dao, times(1)).deleteAllByStatusAndDate(anyInt(), isA(Date.class));
            } catch (Exception e) {
                e.printStackTrace();
                fail("No exceptions must occur");
            }
        }
    }
    

    Commented line doesn't work. Log: Wanted but not invoked: dao.deleteAllByStatusAndDate( , isA(java.util.Date) ); -> at com.nxsystems.dw.publisher.handler.CleanerTaskTest.successfulScenario(CleanerTaskTest.java:52) Actually, there were zero interactions with this mock.

    at com.nxsystems.dw.publisher.handler.CleanerTaskTest.successfulScenario(CleanerTaskTest.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)

    Also when this tets is started debugger doesn't go inside anonymous block. So how to make Mockito go inside anonymous block?

  • Dmitrii Borovoi
    Dmitrii Borovoi over 11 years
    thanks for answer. I've solved this problem by adding annotaion @Transactional to my method so no need to create anonymous methods anymore and test creation became easier.