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 ?

being_j
  • 136
  • 1
  • 11
  • Have a quick look at the [Getting started](http://jmockit.github.io/gettingStarted.html) page. – Rogério Apr 09 '18 at 19:50
  • 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. – being_j Apr 09 '18 at 21:06

3 Answers3

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;
        }};
    }

}
Alfergon
  • 5,463
  • 6
  • 36
  • 56
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;
            }};
        }
Pooja Aggarwal
  • 1,163
  • 6
  • 14
  • 4
    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](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Aug 31 '18 at 06:27
  • 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?](https://stackoverflow.com/help/how-to-answer) – LuFFy Aug 31 '18 at 07:22
  • @LuFFy Edited my answer. – Pooja Aggarwal Aug 31 '18 at 10:34
-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

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
  • 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](https://stackoverflow.com/questions/1906344/should-you-only-mock-types-you-own). – Rogério Apr 10 '18 at 17:05