What is the best way to unit test/integration test the following :
@Service("fooService")
public class FooService {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
/*** Get all **/
@Transactional(readOnly = true)
public List<Foo> getAllFoos() {
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(Foo.class);
return crit.list();
}
}
I am happy to using mockito, but was not sure how leverage its usefulness. Most cases I have seen require the dao/mock dao to be passed in as method parameter.
Obviously I will then extrapolate to more complex methods.
This class is the DAO and there's not much to be gained from mocking Session etc. unless you have a lot of logic in there - if you do then that might be better placed in an actual service class or on the model itself.
Think about what are you be trying to test here: whether these methods return what they should from the database. I would run integration tests against an in-memory database.
If you put the @Resource annotation on a method, it will be much easier to set up a test and configure the service with a mock implementation of a SessionFactory (if that was what you asked for).
@Service("fooService")
public class FooService {
private SessionFactory sessionFactory;
@Resource(name = "sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
I think you have a few things to decide:
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.