Question

I've got problem with Hades Synyx library. I am trying to make some DAO test. My test looks exactly like this below, but when I try to run it with TestNG I've got NPE. Why spring doesn't inject 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()));
    }
}

and my 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>

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

No correct solution

OTHER TIPS

You should implement your

public interface PacientDAO

in some

public class PacientDAOImpl

Then to make this class a candidate for injection, you must add the bean definition to your applicationContext.xml descriptor OR annotate it with the @Repository annotation and activate the annotation based scan in your descriptor with <context:component-scan base-package="your.package"/>

BR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top