Reputation: 580
One of the DAO classes I implemented in a Java EE web project is under
@Repository("ClientsimpleDAO")
public class ClientsimpleDAOImp implements ClientsimpleDAO {
private static final Log log = LogFactory.getLog(ClientsimpleDAOImp.class);
@PersistenceContext
EntityManager em;
@Override
public void delete(Clientsimple clientsimple) {
// TODO Auto-generated method stub
log.debug("removing clientsimple");
try{
em.remove(clientsimple);
log.debug("clientsimple removed");
}
catch(RuntimeException re){
log.error("clientsimple remove failure"+re);
}
}
@SuppressWarnings("unchecked")
@Override
public List<Clientsimple> findByEntreprise(String entreprise) {
// TODO Auto-generated method stub
log.debug("list Cli By entreprise");
try{
Query q =em.createQuery("SELECT cli from Clientsimple cli where cli.entreprise= :entreprise");
q.setParameter(entreprise,entreprise);
List<Clientsimple> cli= (List<Clientsimple>) q.getSingleResult();
return cli;
}catch(RuntimeException re){
log.error(re);
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public List<Clientsimple> findByNom(String nom) {
// TODO Auto-generated method stub
log.debug("list Cli By nom");
try{
Query q =em.createQuery("SELECT cli from Clientsimple cli where cli.nom= :nom");
q.setParameter(nom,nom);
List<Clientsimple> cli= (List<Clientsimple>) q.getSingleResult();
return cli;
}catch(RuntimeException re){
log.error(re);
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public List<Clientsimple> findByPrenom(String prenom) {
// TODO Auto-generated method stub
log.debug("list Cli By prenom");
try{
Query q =em.createQuery("SELECT cli from Clientsimple cli where cli.prenom= :prenom");
q.setParameter(prenom,prenom);
List<Clientsimple> cli= (List<Clientsimple>) q.getSingleResult();
return cli;
}catch(RuntimeException re){
log.error(re);
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public List<Clientsimple> findByRegion(String region) {
// TODO Auto-generated method stub
log.debug("list Cli By region");
try{
Query q =em.createQuery("SELECT cli from Clientsimple cli where cli.regioncli= :region");
q.setParameter(region,region);
List<Clientsimple> cli= (List<Clientsimple>) q.getSingleResult();
return cli;
}catch(RuntimeException re){
log.error(re);
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public List<Clientsimple> getALL() {
// TODO Auto-generated method stub
log.debug("list ALL Cli");
try{
Query q =em.createQuery("SELECT cli from Clientsimple cli");
List<Clientsimple> cli= (List<Clientsimple>) q.getSingleResult();
return cli;
}catch(RuntimeException re){
log.error(re);
return null;
}
}
@Override
public void save(Clientsimple clientsimple) {
// TODO Auto-generated method stub
log.debug("save clientsimple");
try{
em.persist(clientsimple);
log.debug("clientsimple saved");
}
catch(RuntimeException re){
log.error("clientsimple saving failure"+re);
}
}
@Override
public void update(Clientsimple clientsimple) {
// TODO Auto-generated method stub
log.debug("update clientsimple");
try{
em.merge(clientsimple);
log.debug("clientsimple merged");
}
catch(RuntimeException re){
log.error("clientsimple merging failure"+re);
}
}
}
so, i don't know how can i test this dao or other ones?
i have created a main class to test it but it gives me an error(see the image under it contains code and the error in the console).
the following image show my project hierarchy(technologies used flex, spring, jpa, hibernate);
Upvotes: 1
Views: 1513
Reputation: 298818
Don't test with main classes, use Spring's test framework. Read about it in the section 9.3 Integration testing.
Let your Test class inherit from one of the Spring support classes described here, e.g. AbstractTransactionalJUnit4SpringContextTests
, add the context configuration and some dependencies and do some testing. Simple enough.
@ContextConfiguration("classpath:path/to/your/spring/context.xml")
public class YourServiceTest extends
AbstractTransactionalJUnit4SpringContextTests{
@Autowired
private YourDaoInterfaceHere dao;
// method is automatically transactional
@Test
public void testSomething(){
dao.persist(someData);
dao.load(someOtherData);
}
}
The key is to
daoContext.xml
, but not everythingContext.xml
)PropertyPlaceHolderConfigurer
or PropertyOverrideConfigurer
mechanisms to use different environments in test and productionAnd as a side note:
catch(RuntimeException re){
log.error("clientsimple remove failure"+re);
}
You should never log an exception like this. You are losing the stack trace. Always use the log.error(message, throwable)
versions.
Upvotes: 2
Reputation: 1628
Your main class doesn't start a Spring context, so your EntityManager is null. You have to load your Spring context and then Spring will autowire your EntityManager.
Upvotes: 0
Reputation: 61538
Our team had this integration problem too. The problem seems to be that you can't really unit test classes so heavily relying on the application server/container.
We ended up giving up unit testing for our DAOs and EJBs and are testing them using integration tests - the output of a DAO is tested via the services that uses the DAO.
Upvotes: 0