我是Mockito框架的新手,我尝试在dao层中模拟方法,然后将模拟的bean注入服务层。但是它达到了实际的db。我在下面给出了示例程序。我需要模拟sessionfactory吗,但会话对象为空值。有人可以帮忙解决
public class MockitoTest {
@Mock
EmpProfileDao profDao;
@InjectMock
EmpProfileService profDService;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testEmployee(){
Emp e=new Emp();
when(profDao.getEmpData("data")).thenReturn(e);
profDService.getEmpData("data");
}
}
@service
@transactional
public class EmpServiceImpl implements EmpProfileservice{
@AutoWired
EmpProfileDao profDao;
public EmpServiceImpl (EmpProfileDao profDao){
this.profDao=profDao;
}
public Emp getEmpData(String data){
Emp e=profDao.getEmpData(data);
return e;
}
@Repository
class EmpProfileDaoImpl implements EmpProfileDao {
public Emp getEmpData(data){
// criteria Query
}