I have already do the test using AbstractDependencyInjectionSpringContextTests and it works but in spring 3 it is deprecated, so I decided to try @ContextConfiguration but spring say that default constructor is not found, I check and the class doesn't have any constructor.
If I use this test spring give the object.
package atoms.portales.servicios.impl;
import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
/**
*
* @author tsalazar
*/
public class ClienteServiceImplDeTest extends AbstractDependencyInjectionSpringContextTests{
private ClienteService clienteService;
public ClienteService getClienteService() {
return clienteService;
}
public void setClienteService(ClienteService clienteService) {
this.clienteService = clienteService;
}
public ClienteServiceImplDeTest(String testName) {
super(testName);
}
@Override
protected String[] getConfigLocations() {
return new String[]{"PersistenceAppCtx.xml", "ServicesAppCtx.xml"};
}
/**
* Test of buscaCliente method, of class ClienteServiceImplDeTest. */
public void testBuscaCliente() {
System.out.println("=======================================");
System.out.println("buscaCliente");
String nombre = "";
System.out.println(clienteService);
System.out.println("=======================================");
}
}
But if I use this, spring say that default constructor is not found.
package atoms.config.portales.servicios.impl;
import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
/**
*
* @author tsalazar
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/PersistenceAppCtx.xml", "/ServicesAppCtx.xml"})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class ClienteServiceImplTest {
@Autowired
private ClienteService clienteService;
/**
* Test of buscaCliente method, of class ClienteServiceImpl. */
@Test
public void testBuscaCliente() {
System.out.println("=======================================");
System.out.println("buscaCliente");
System.out.println(clienteService);
System.out.println("=======================================");
}
}
This how I do the implementacion:
package atoms.portales.servicios;
import atoms.portales.model;
/**
* Una interface para obtener clientes, con sus surcursales, servicios, layouts
* y contratos. Tambien soporta operaciones CRUD.
* @author tsalazar
*/
public interface ClienteService {
/**
* Busca clientes a partir del nombre
* @param nombre
*/
public Cliente buscaCliente(String nombre);
}
the implemetacion
package atoms.portales..servicios.impl;
import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* A JPA-based implementation.Delegates to a JPA entity manager to issue data access calls
* against the backing repository. The EntityManager reference is provided by the managing container (Spring)
* automatically.
*/
@Service("clienteSerivice")
@Repository
public class ClienteServiceImpl implements ClienteService {
public ClienteServiceImpl() {
}
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Transactional(readOnly = true)
public Cliente buscaCliente(String nombre) {
Cliente cliente = em.getReference(Cliente.class, 1l);
return cliente;
}
}
spring configuration:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
<tx:annotation-driven />
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<!-- Deploys a in-memory "booking" datasource populated -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<context:component-scan base-package="atoms.portales.servicios" />
</beans>
This is the persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="configuradorPortales" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>atoms.portales.model.Cliente</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
</properties>
</persistence-unit>
</persistence>
This is the error that give me: