How to mock JdbcTemplate.update using Jmockit?

I am new to Jmockit and I am trying to make fun of jdbcTemplate.udpate() using the following check,

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

flushUpdate has an update request,

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

The test should check if the update request is run twice.

But I get 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 have any ideas?

0
source share
2 answers

Please show your full test code.

In any case, 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; }}; } } 
+1
source

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

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

-1
source

All Articles