我无法在 Spring 启动应用程序中使用 mockito 模拟 Dao 方法
I am not able to mock the Dao method using mockito in Spring boot application
我无法在 spring 引导中模拟 Dao 方法。请让我知道我在下面的代码中做错了什么。
我尝试使用 SpringJUnit4ClassRunner 并模拟 Dao 方法。但是,它仍然进入 Dao 方法而不是返回模拟值。
我也尝试过使用 MockitoJUnitRunner,但当时无法调用服务方法,因为它获得了空值。
@RestController
public class HomeController {
@Autowired
HomeSeriveInterface service;
@Autowired
HomeDaoImpl homeDao;
@GetMapping(value="/getData")
public String Data() {
System.out.println("Inside Controller");
List < Map < String, Object >> rows = service.getData();
return "Hi Yogita" + rows;
}
}
@Service
public class HomeService implements HomeSeriveInterface{
@Autowired
HomeDao dao;
@Override
public List<Map<String, Object>> getData() {
System.out.println("Inside Service");
return dao.getData();
}
}
@Repository
public class HomeDaoImpl implements HomeDao{
@Autowired
@Qualifier("jdbcTemplate1")
private JdbcTemplate jdbcTemplate;
@Override
public List < Map < String, Object >> getData() {
System.out.println("Inside Dao");
List < Map < String, Object >> rows = jdbcTemplate.queryForList("SELECT * FROM COURCES");
return rows;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class CcdWebApplicationTests {
@InjectMocks
@Autowired
HomeController homeController;
@Mock
HomeDao homeDao;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getDataTest() {
System.out.println("testing *******");
List < Map < String, Object >> data = null;
Mockito.when(homeDao.getData())
.thenReturn(data);
System.out.println("2nd *");
String data2 = homeController.Data();
System.out.println(data2);
}
}
您不需要 @InjectMocks
并使用 @MockBean
而不是 @Mock
:
@Autowired
HomeController homeController;
@MockBean
HomeDao homeDao;
您也不需要这部分:
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
我无法在 spring 引导中模拟 Dao 方法。请让我知道我在下面的代码中做错了什么。 我尝试使用 SpringJUnit4ClassRunner 并模拟 Dao 方法。但是,它仍然进入 Dao 方法而不是返回模拟值。 我也尝试过使用 MockitoJUnitRunner,但当时无法调用服务方法,因为它获得了空值。
@RestController
public class HomeController {
@Autowired
HomeSeriveInterface service;
@Autowired
HomeDaoImpl homeDao;
@GetMapping(value="/getData")
public String Data() {
System.out.println("Inside Controller");
List < Map < String, Object >> rows = service.getData();
return "Hi Yogita" + rows;
}
}
@Service
public class HomeService implements HomeSeriveInterface{
@Autowired
HomeDao dao;
@Override
public List<Map<String, Object>> getData() {
System.out.println("Inside Service");
return dao.getData();
}
}
@Repository
public class HomeDaoImpl implements HomeDao{
@Autowired
@Qualifier("jdbcTemplate1")
private JdbcTemplate jdbcTemplate;
@Override
public List < Map < String, Object >> getData() {
System.out.println("Inside Dao");
List < Map < String, Object >> rows = jdbcTemplate.queryForList("SELECT * FROM COURCES");
return rows;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class CcdWebApplicationTests {
@InjectMocks
@Autowired
HomeController homeController;
@Mock
HomeDao homeDao;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getDataTest() {
System.out.println("testing *******");
List < Map < String, Object >> data = null;
Mockito.when(homeDao.getData())
.thenReturn(data);
System.out.println("2nd *");
String data2 = homeController.Data();
System.out.println(data2);
}
}
您不需要 @InjectMocks
并使用 @MockBean
而不是 @Mock
:
@Autowired
HomeController homeController;
@MockBean
HomeDao homeDao;
您也不需要这部分:
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}