Hades Synyx图书馆。 DAO测试,Autowired给出了NPE

时间:2014-03-02 12:13:45

标签: java spring

我遇到了Hades Synyx库的问题。我正在尝试进行一些DAO测试。我的测试看起来如下所示,但是当我尝试使用TestNG运行它时,我已经获得了NPE。为什么春天不注射PacientDAO?

@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class PacientDAOTest {

@Autowired
private PacientDAO dao;

@Test
public void shouldAddPacient() {
    Pacient pacient = new Pacient();
    pacient.setName("x");
    pacient.setSecondName("xx");
    pacient.setLastName("xxx");
    pacient.setBirthDate(new Date(2000, 10, 10));
    pacient.setPesel(1111111111);
    pacient.setSex(Sex.FEMALE);
    pacient.setEmail("aaza@asd.pl");
    pacient.setContactNumber(123456L);
    dao.save(pacient);
    Assert.assertEquals(pacient, dao.readByPrimaryKey(pacient.getId()));
    }
}

和我的pacientDAO:

public interface PacientDAO extends GenericDao<Pacient, Long> {

@Query("Select * from dietetyk_pacients where name = :name")
    public Pacient getByName(@Param("name") String name);
}

的applicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"     xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:hades="http://schemas.synyx.org/hades"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc     http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd">

    <context:annotation-config />
    <context:component-scan base-package="pl.arprojects.dietetyk.api"/>
    <tx:annotation-driven transaction-manager="transactionManager" />

    <import resource="classpath:META-INF/dao-context.xml" />
</beans>

道context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:hades="http://schemas.synyx.org/hades"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd">

    <import resource="infrastructure.xml" />

    <!-- Custom configuration for the custom implementation based on JDBC -->   
    <hades:dao-config base-package="pl.arprojects.pl.api.dao" />

</beans>

infrastructure.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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:hades="http://schemas.synyx.org/hades"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd">
    <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 name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="database" value="MYSQL" />
            </bean>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/d2" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <hades:dao-config base-package="pl.arprojects.dietetyk.api.domian" />
</beans>

1 个答案:

答案 0 :(得分:0)

您应该实施

public interface PacientDAO

在某些

public class PacientDAOImpl

然后,要使此类成为注入候选者,必须将bean定义添加到 applicationContext.xml 描述符 OR 使用@Repository注释对其进行注释并使用<context:component-scan base-package="your.package"/>

在您的描述符中激活基于注释的扫描

BR。