Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test anonymous methods with JUnit or Mockito?

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?

like image 937
Dmitrii Borovoi Avatar asked Aug 15 '12 00:08

Dmitrii Borovoi


People also ask

How to use Mockito In JUnit?

The most popular way of using Mockito is within the JUnit framework to help you write better tests. For starters, mocking in unit testing is used for isolating the AUT (Application Under Test) from external dependencies.

How do you Mock a list In JUnit?

Initialising a mock As described in Mockito’s documentation a way to mock some object is: List mockedList = mock (List.class); Another way, that is used in current examples is to annotate the filed that is going to be mocked with @Mock and annotate JUnit test class with @RunWith (MockitoJUnitRunner.class).

How to verify whether a method is being called in Mockito?

That is why we can only verify whether that method is being called or not. Mockito provides us with a verify () method that lets us verify whether the mock void method is being called or not. It lets us check the number of methods invocations.

How to use the argumentmatchers in Mockito?

To use the ArgumentMatchers in your test class, you have to first import the ‘ArgumentMatchers` class from Mockito as follows: import static org.mockito.ArgumentMatchers.*; Many argument matchers can be defined in the ArgumentMatcher class. Some of them are described below: The any argument matcher receives an object of type Class as an argument.


2 Answers

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().

like image 84
Mateusz Dymczyk Avatar answered Sep 23 '22 05:09

Mateusz Dymczyk


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.

like image 30
Rrr Avatar answered Sep 23 '22 05:09

Rrr