In the test case for service,I am unable to resolve dependency of dao class and JdbcTemplate.
public class TestPromotionUsingJunit {
@InjectMocks
private ItemService itemService;
@Mock
private ItemDAOImpl itemDAOImpl;
@Mock
private JdbcTemplate jdbcTemplate;
@Before
public void setupMock() {
MockitoAnnotations.initMocks(this);
itemService = new ItemService();
}
@Test
public void testFindMax() {
Product pro1 = new Product();
pro1.setPluCode("4900692627408");
pro1.setCategoryNo("2");
pro1.setCategoryName("Women");
pro1.setProductName("T-Shirt10163");
pro1.setColor("CY");
pro1.setSize("32");
BigDecimal b1 = new BigDecimal(94.00);
BigDecimal b2 = new BigDecimal(8);
pro1.setPrice(b1);
pro1.setTax(b2);
Product pro2 = new Product();
pro2.setPluCode("4900692627408");
assertEquals(pro1.getPrice(), itemService.getItem(pro1));
}
}
Here ItemService should return product object but it returns null. Due to internally unable to solve dependency.
@InjectMocks creates a mock instance of itemService, but then
itemService = new ItemService();
creates a real instance and throws the mock away. In setupMock(), after itemService is constructed, try adding the equivalent of this:
itemService.setItemDao(itemDAOImpl);
So you'll have a real ItemService which uses a mock DAO.
Then in testFindMax() configure the DAO to return pro1, something like this:
when(itemDAOImpl.getItem(...)).thenReturn(pro1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.