Boa Noite…
Estou tentando realizar testes automaticos com minhas classes DAOs, porém quando utilizo a anotação @autowired do SPRING para invocar a DI, ocorre erro e não é realizado os testes…Alguém sabe como posso resolver este problema;;
Obrigado…
package br.com.alacarte.unitTests.DAO;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import br.com.alacarte.dao.EstadoDAO;
import br.com.alacarte.model.Estado;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:**/applicationContext*.xml"})
public class TesteEstadoDAO {
private EstadoDAO estadoDao;
private String nome = "Sao Paulo";
@Autowired
public void setEstadoDao (EstadoDAO estadoDao){
this.estadoDao = estadoDao;
}
private Estado getEstado(){
Estado est = new Estado();
est.setNome("Sao Paulo");
est.setEstado("SP");
return est;
}
@Test
public void testSalvar() {
Estado estado = null;
estado = estadoDao.salvar(getEstado());
assertNotNull(estado);
assertEquals("Sao Paulo", estado.getNome());
}
@Test
public void testAtualizar() {
Map<String,Object> params = new HashMap<String, Object>();
params.put("nome", nome);
Estado estado = estadoDao.pesqParam("select e from estado e where e.nome = :nome", params);
estado.setNome("Minas Gerais");
estado = estadoDao.atualizar(estado);
assertNotNull(estado);
assertEquals("Minas Gerais", estado.getNome());
}
@Test
public void testListaTodos() {
List<Estado> estado = estadoDao.listaTodos();
assertNotNull(estado);
assertEquals(1, estado.size());
assertEquals("Minas Gerais", estado.get(0).getNome());
}
@Test
public void testExcluir() {
Map<String,Object> params = new HashMap<String, Object>();
params.put("nome", nome);
Estado estado = estadoDao.pesqParam("select e from estado e where e.nome = :nome", params);
estadoDao.excluir(estado);
assertNull(estadoDao.pesqParam("select e from estado e where e.nome = :nome", params));
}
}
Como erro apresenta a seguinte mensagem…
TestContextManager:234 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1db05b2] to prepare test instance [br.com.alacarte.unitTests.DAO.TesteEstadoDAO@530cf2]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in file [C:\Users\Duh\workspaceGanymede\GestaoPizza\bin\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name ‘GestaoPizza’ found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)