I'm running a JUnit test on a function returning a list of results for me and I'm trying to mock the database call being done with an autowired JdbcTemplate object. For the privacy of my company, I'm replacing all the fields and databases in my queries with generic {labels} but rest assured that I double checked and triple checked and had another set of eyes look at the sql to make sure it's the same in every spot in the process. Hoping someone has some ideas for me to try to solve this issue, I'm completely stumped.
Here's the set up in my @before function of init():
wfDao = new WfDao();
JdbcTemplate template = EasyMock.createMock(JdbcTemplate.class);
EasyMock.expect(template.queryForList("select DISTINCT {my field} from {my database} where {field1}=? and {field2}=?", String.class, 33, "I")).andReturn(resultsList).anyTimes();
EasyMock.replay(template);
wfDao.setJdbcTemplate(template);
I debugged into my test and function and checked that the sql being actually used it exact to what I have in my test class, as well as the other 3 variables being passed. Here is my test:
@Test
public void getWfEntityLabels()
{
List<String> stuffs = wfDao.getWfEntityLabels(33, "I");
assertTrue(stuffs.size() > 0);
}
Here's the function getWfEntityLabels:
public List<String> getWfEntityLabels(long field1, String field2)
{
final String sql = "select DISTINCT {my field} from {my database} where {field1}=? and {field2}=?";
return jdbcTemplate.queryForList(sql, String.class, field1, field2);
}
And when I run this, I get the following:
java.lang.AssertionError: Unexpected method call JdbcTemplate.queryForList("select DISTINCT {my field} from {my database} where {field1}=? and {field2}=?", class java.lang.String, 33, "I"):
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97)
at org.springframework.jdbc.core.JdbcTemplate$$EnhancerByCGLIB$$d346dc18.queryForList(<generated>)
at com.monsanto.mbt.minion.repository.WfDao.getWfEntityLabels(WfDao.java:29)
at com.monsanto.mbt.minion.repository.WfDaoTest.getWfEntityLabels(WfDaoTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Any help would be much appreciated! Thanks!
The solution is simple and you might bite your cheek when you see it.
Just put 33L
instead of 33
in the expect. queryForList
takes objects in parameters so 33
will be boxed to new Integer(33)
and remember as is by EasyMock. Then, your call is passing a long to queryForList
. Mismatch!