Spring 3.0中的@ContextConfiguration给我找不到默认构造函数

时间:2010-05-06 16:38:26

标签: java unit-testing spring

我已经使用AbstractDependencyInjectionSpringContextTests进行测试并且它可以正常工作但是在第3季它已被弃用,所以我决定尝试@ContextConfiguration但是春天说没有找到默认构造函数,我检查并且该类没有任何构造函数

如果我使用此测试弹簧给出物体。

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("=======================================");



        }


    }

但是如果我使用它,那么Spring会说找不到默认的构造函数。

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("=======================================");



    }
}

这是我如何做的实现:

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);



    }

实施

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;

    }


}

弹簧配置:

<?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>

这是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>

这是给我的错误:

1 个答案:

答案 0 :(得分:0)

抱歉,我注意到wath正在发生,当我进行测试时,我将ClienteServiceImplDeTest的名称更改为ClienteServiceImpl,因为我只想检查新测试,但这会干扰服务实现。我在同一个包中有两个具有相同名称的类,就是这样。