My question is similar to this SO question
I have two JdbcTemplate.queryForObject(..,...,...,)
calls in service like below,
depositPostedAmt = jdbcTemplate.queryForObject(Constants.FETCH_DEPOSIT_POSTED_AMT, BigDecimal.class,new Object[] { remitBean.getDepositId() });
and
claimPostedAmt =jdbcTemplate.queryForObject(Constants.FETCH_CLAIM_POSTED_AMOUNT,BigDecimal.class, new Object[] { claim.getClaimId(), remitBean.getContractNum() });
Third argument, new Object[]
is different among those two calls and actual sql String differs.
So I am trying to use to different mockings to return two different objects in both scenario as shown below ,
when(jdbcTemplate.queryForObject(eq(Constants.FETCH_DEPOSIT_POSTED_AMT), eq(BigDecimal.class), anyObject())).thenReturn(depositPostedAmt);
when(jdbcTemplate.queryForObject(eq(Constants.FETCH_CLAIM_POSTED_AMOUNT), eq(BigDecimal.class), anyObject())).thenReturn(claimPostedAmt);
i.e. I wish to receive two different BigDecimal
on two different queries.
I receive depositPostedAmt
as expected but claimPostedAmt
is always null even though I have initialized it in @Before method same as depositPostedAmt
so I am guessing that my when
matcher doesn't found any match. I have tried various syntax for third argument matching like any(Object[].class)
and anyRef(objectArray)
etc but second time, I always get NULL.
I am not sure as what I am missing as there are no errors. I am using JUnit along with Mockito 1.9.5.
Here is sample code - everything works OK except that claimPostedAmt
remains null in called service.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
public class RCMatchDaoImplTest{
@Autowired private RCMatchDaoImpl service;
@Autowired private JdbcTemplate jdbcTemplate;
@Autowired private Logger logger;
private RemitBean remitBean;
private List<RemitBean> remitBeanList;
private BigDecimal depositPostedAmt,claimPostedAmt,remitAmount;
private ClaimVO claim;
private List<ClaimVO> claims;
@Before
public void setUp() throws NoSuchFieldException, SecurityException, Exception{
/* Set dependencies*/
service.setJdbcTemplate(jdbcTemplate);
setFinalStatic(RCMatchDaoImpl.class.getDeclaredField("logger"),logger);
remitBean = new RemitBean();
remitBeanList=new ArrayList<>();
claim= new ClaimVO();
claims= new ArrayList<>();
remitBeanList.add(remitBean);
depositPostedAmt=new BigDecimal(-10);
claimPostedAmt = new BigDecimal(-10);
remitAmount=new BigDecimal(20);
claims.add(claim);
}
private static void setFinalStatic(Field field, Object newValue) throws Exception{
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
@Test
public void testLucenePost_refund_success() throws SQLException{
/* Set Data */
remitBean.setRemitType("R");
remitBean.setRemitAmt(remitAmount);
remitBean.setDepositId(6866418);
remitBean.setClaims(claims);
depositPostedAmt=depositPostedAmt.add(new BigDecimal(20));
claimPostedAmt=claimPostedAmt.add(new BigDecimal(10));
claim.setClaimId(6866418);
claim.setContractNum("100");
Object[] depositParams = new Object[] { 6866418 };
Object[] claimParams = new Object[] { 6866418,"100" };
/* Record Invocations*/
when(jdbcTemplate.queryForObject(eq(Constants.FETCH_DEPOSIT_POSTED_AMT), eq(BigDecimal.class), anyObject())).thenReturn(depositPostedAmt);
when(jdbcTemplate.queryForObject(eq(Constants.FETCH_CLAIM_POSTED_AMOUNT), eq(BigDecimal.class), anyObject())).thenReturn(claimPostedAmt);
doNothing().when(logger).error(anyString());
/* Play the Service */
service.lucenePost(remitBeanList);
/* Verify Results */
/* reset data to original value as in SetUp method*/
}
Just for sake of completeness, here is my context class too,
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
public class TestConfig {
@Mock JdbcTemplate jdbcTemplate;
@Mock Logger logger;
TestConfig(){
MockitoAnnotations.initMocks(this);
}
@Bean
public RCMatchDaoImpl getRCMatchDaoImpl() {
return new RCMatchDaoImpl();
}
@Bean
public JdbcTemplate jdbcTemplate(){
return jdbcTemplate;
}
@Bean
public Logger logger(){
return logger;
}
}