Pessoal estou configurando um projeto que usa o spring e para fazer a parte de testes unitarios iremos usar o TesteNG.
Tem uma arquitetura em camadas com a seguinte representação DAO -> SERVICE -> WEB .
O teste ficaria na camada de SERVICE, testando todos os serviços publicos do sistema.
O problema é que para os componentes spring do service no teste o contexto do spring é criado numa boa e faz a injeção de dependencia e tudo mais. Porém quando estamos falando de um Service que dentro dele tem um @Autowired para o DAO por exemplo ai não rola, da falha ao entender o application-context do teste.
Isso pq o DAO está em um outro projeto que é referenciado no projeto SERVICE e também o DAO carrega o persistenceContext, acho que por ai o motivo mas não vejo ideia de como solucionar isso.
Vejam meus arquivos:
Application-context-text.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="br.com.techforti.arqstruts2.service" annotation-config="true" />
<context:component-scan base-package="br.com.techforti.arqstruts2.service.impl" annotation-config="true" />
<context:component-scan base-package="br.com.techforti.arqstruts2.service.test" annotation-config="true" />
</beans>
Teste
package br.com.techforti.arqstruts2.service.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
import br.com.techforti.arqstruts2.model.TesteModel;
import br.com.techforti.arqstruts2.service.TesteService;
@Test(groups={"V.1.0"})
@ContextConfiguration(locations={"/Application-context-text"})
public class TesteCaseTestNGTest extends AbstractTestNGSpringContextTests {
@Autowired
private TesteService vs;
public void testInjecaoSpring(){
Assert.assertNotNull(vs,"O VendaService Não pdoe ser null");
}
public void testParametroVendaNull(){
TesteModel teste = vs.salvarTeste(null);
Assert.assertNotNull(teste);
}
public void testParametroVendedorNull(){
TesteModel testeModel = new TesteModel();
vs.salvarTeste(testeModel);
}
}
Quando o TesteServiceImpl está assim roda na boa:
@Service
public class TesteServiceImpl implements TesteService {
//@Autowired
//private TesteDAO testeDAO;
public List<TesteModel> listarTeste() {
//return testeDAO.listar();
return new ArrayList<TesteModel>();
}
public TesteModel salvarTeste(TesteModel testeModel) {
//return this.testeDAO.salvar(testeModel);
return new TesteModel();
}
}
Porém quando está assim não roda:
@Service
public class TesteServiceImpl implements TesteService {
@Autowired
private TesteDAO testeDAO;
public List<TesteModel> listarTeste() {
//return testeDAO.listar();
return new ArrayList<TesteModel>();
}
public TesteModel salvarTeste(TesteModel testeModel) {
//return this.testeDAO.salvar(testeModel);
return new TesteModel();
}
}
Bom eh isso, nao vejo ideia =/ … Caso alguem tenha alguma ideia por favor será muito bem vinda.
Abraços,