How to mock JdbcTemplate.update using Jmockit?
Asked Answered
A

3

0

I'm new to Jmockit and I'm trying to mock jdbcTemplate.udpate() using the following verification,

    new Expectations() {{
        someRef.flushUpdates();
    }};

    new Verifications() {{
        String query;
        jdbcTemplate.update(query = withCapture(), withInstanceOf(Date.class));
        times = 1;
    }};

The flushUpdate has an update query,

public void flushUpdates(){
  Date now = new Date();
  String query = "Update table_name set last_updated = ? ";
  jdbcTemplate.update(query,now);
}

The test is to verify if update query is triggered twice.

But I'm getting the following error.

mockit.internal.MissingInvocation: Missing 1 invocations to:
org.springframework.jdbc.core.JdbcTemplate#update(String, Object[])
with arguments: any String, an instance of java.util.Date
on mock instance: org.springframework.jdbc.core.JdbcTemplate@2d000e80

Does anyone has any idea ?

Acetous answered 9/4, 2018 at 15:12 Comment(2)
Have a quick look at the Getting started page.Tonl
I was using the @Injectable JdbcTemplate jdbcTemplate; in the base class and also in the test class which was causing the object reference to change when called from the Expectation inner class. Removing the reference from base class fixes the issue.Acetous
N
1

Please, show your complete test code.

Either way, I think that in this case you need to do something like:

@RunWith(JMockit.class)
public class Test{

    @Tested
    private SomeClass someRef;

    @Injectable
    private JbdcTemplate jdbcTemplate;

    @Test
    public void test(){
        someRef.flushUpdates();

        new Verifications() {{
            String query;
            jdbcTemplate.update(query = withCapture(), withInstanceOf(Date.class));
            times = 1;
        }};
    }

}
Neustria answered 10/4, 2018 at 10:40 Comment(0)
O
1

mockit.internal.MissingInvocation: Missing 1 invocations to: is thrown when your method parameters do not match. So when you use 'any' keyword it does not look for an exact match while invoking the mocked method.

@Test
        public void test(){
            someRef.flushUpdates();

            new Verifications() {{
                String query;
                jdbcTemplate.update((String)any, (Date)any);
                times = 1;
            }};
        }
Overhasty answered 31/8, 2018 at 6:3 Comment(3)
While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.Gerius
While your code may correct as answer But Elaborating what your code does, It can improve the quality of your answer. Checkout the Article : How do I write a good answer?Moffatt
@Moffatt Edited my answer.Overhasty
T
-1

Your job would be simpler if, rather than mocking jdbcTemplate you would encapsulate calls to jdbcTemplate in DAO classes and mock dao instead.

There is a rule do not mock API you do not own (it applies to any mocking technology) https://github.com/mockito/mockito/wiki/How-to-write-good-tests

Tome answered 10/4, 2018 at 10:52 Comment(1)
I wouldn't mock JdbcTemplate (or a DAO, if any) myself, but there is nothing inherently wrong in doing it, if you really want to mock database access. See the relevant question.Tonl

© 2022 - 2024 — McMap. All rights reserved.