Announcement Announcement Module
Collapse
No announcement yet.
Problem with Junit and @Autowired Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Junit and @Autowired

    Hello everyone, this is my first post here.

    I'm developing a browser game with Flex, Blazeds and Spring, JPA/Hibernate. Tomcat, MySql and Eclipse. I'm also using this technology for teaching purposes.

    I want to use DDD and i have a problem with Junit.

    Here my test class:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:*application-context.xml", "classpath:*flex-servlet.xml" })
    public class UserServiceImplTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindByUsername() {
    User user = userRepository.findByUsername("user");
    assertNotNull(user.getUserProfile());
    }
    }

    Under /WEB-INF/spring i have application-context.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans ...>

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

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/evolutionDB" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="evolutionJPA" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="database" value="MYSQL" />
    <property name="showSql" value="true" />
    </bean>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    </beans>

    and flex-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans ...>
    <security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled" />

    <context:annotation-config />

    <context:component-scan base-package="it.cpf.evolutionserver">
    <context:include-filter type="regex" expression=".*ServiceImpl"/>
    <context:include-filter type="regex" expression=".*RepositoryImpl"/>
    </context:component-scan>

    <flex:message-broker>
    <flex:secured />
    </flex:message-broker>

    <bean id="userRepository" class="it.cpf.evolutionserver.repository.user.User RepositoryImpl" />

    <bean id="userService" class="it.cpf.evolutionserver.service.user.UserSer viceImpl">
    <property name="userRepository" ref="userRepository" />
    <flex:remoting-destination destination-id="userDestination" />
    </bean>

    </beans>

    My web.xml is
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring/*-context.xml
    </param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <listener>
    <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <servlet>
    <servlet-name>flex</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/flex-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>flex</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFil terProxy</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEnti tyManagerInViewFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
    <welcome-file>/index.html</welcome-file>
    </welcome-file-list>

    </web-app>

    My UserRepositoryImpl is

    @Repository
    public class UserRepositoryImpl implements UserRepository {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
    }

    @Override
    public User findByUsername(String username) throws NoResultException {
    try {
    return entityManager.find(User.class, username);
    } finally {
    entityManager.close();
    }
    }
    }

  • #2
    I'm having the same problem.
    Managed to solve?

    Comment

    Working...
    X