首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在同一个多模块maven项目中将bean从一个maven模块自动绑定到另一个maven模块。

问在同一个多模块maven项目中将bean从一个maven模块自动绑定到另一个maven模块。
EN

Stack Overflow用户
提问于 2014-04-27 05:28:53
回答 1查看 1.9K关注 0票数 0

我有一个带有折叠子模块的多模块maven项目。

代码语言:javascript
复制
Tracker
|--Tracker-core
|--Tracker-dao
|  |---src/main/resource/spring-dao-config.xml
|
|--Tracker-services
|  |---src/main/resource/spring-service-config.xml
|
|--Tracker-web

在跟踪器-道中,我在资源包中有一个Springcontext.xml。这将扫描dao类,并包括其他数据源配置。

spring-dao-config.xml:

代码语言:javascript
复制
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.gits.tracker"></context:component-scan>

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:/database.properties" />
    </bean>

    <import resource="classpath:hibernate-config.xml" />

    <!-- Declare a datasource that has pooling capabilities -->

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- An AnnotationSessionFactoryBean for loading Hibernate mappings from 
        annotated domain classes. -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.gits.tracker.core.entity.Address</value>
                <value>com.gits.tracker.core.entity.Company</value>
                <value>com.gits.tracker.core.entity.Customer</value>
                <value>com.gits.tracker.core.entity.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop> -->
            </props>
        </property>
    </bean>    

</beans>

仅针对dao层的Junit测试是非常正常的。

在跟踪服务中,我使用这个跟踪核心作为依赖项。在Tracker-service中运行Junit时,它会出错,表示加载应用程序上下文失败,未能找到至少一个与名称匹配的bean。

spring-service-config.xml

代码语言:javascript
复制
<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- <import resource="classpath:spring-dao-config.xml"/> -->
    <context:component-scan base-package="com.gits.tracker.service.services"></context:component-scan>
</beans>

Junit in Tracker-service

问题在这里:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations ={“classpath:service config.xml”})

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-service-config.xml" })
public class TestUserService extends
        AbstractTransactionalJUnit4SpringContextTests {

    private static Logger logger;
    private static ApplicationContext ctx;
    private UserService userService;

    @BeforeClass
    public static void init() {
        logger = Logger.getLogger(User.class);
        if (logger.isDebugEnabled()) {
            logger.debug("IN DEBUG MODE");
        }
    }

    @Before
    public void localInit() {
        logger.info("*************EXECUTING LOCALINIT*************");
        ctx = new FileSystemXmlApplicationContext(
                "src/main/resources/spring-config.xml");
        userService = (UserService) ctx.getBean("userServiceImpl");

        // userDao.executeQuery("delete from User where id<>3");
        logger.info("*************DELETED ALL COMPANY FROM TABLE*************");
        logger.info("*************EXITING OUT OF LOCALINIT*************");
    }

    @AfterClass
    public static void stop() {
        logger.debug("TEST COMPLETED");
    }

    private UserServiceImpl loadUserService() {
        return (UserServiceImpl) ctx.getBean("userServiceImpl");
    }

    private UserDTO createTestUserDTO() {
        UserDTO dto = new UserDTO("manoj", "manojpass", "true");
        return dto;
    }

    @Test
    public void testCreateUser() {
        loadUserService();
        UserDTO dto = createTestUserDTO();
        Long id = userService.createUser(dto);
        dto.setId(id);
        UserDTO dto_1 = userService.getUserById(id);
        org.junit.Assert.assertEquals(dto.toString(), dto_1.toString());
    }    


    @Test
    public void findByCriteriaWithAlias() {
        loadUserService();
        UserDTO dto = createTestUserDTO();
        Long id = userService.createUser(dto);
        CriteriaWithAlias criteriaWithAlias = new CriteriaWithAlias();
        HashMap<String, String> alias = new HashMap<String, String>();
        List<Criterion> criterions = new ArrayList<Criterion>();
        Criterion criterion0 = Restrictions.eq("username", dto.getUsername());
        criterions.add(criterion0);
        criteriaWithAlias.setAlias(alias);
        criteriaWithAlias.setCriterions(criterions);

List<UserDTO> users = userService
                .findByCriteriaWithAlias(criteriaWithAlias);
for (UserDTO user : users) {
org.junit.Assert.assertFalse(user.getPassword().isEmpty());
org.junit.Assert.assertFalse(user.getId().toString().isEmpty());
org.junit.Assert.assertFalse(user.getUsername().isEmpty());
org.junit.Assert.assertFalse(user.getEnabled().isEmpty());
}
}

    @Test
    public void findByProjection() {
            loadUserService();
    UserDTO dto = createTestUserDTO();
    userService.createUser(dto);
    CriteriaWithAlias criteriaWithAlias = new CriteriaWithAlias();
    HashMap<String, String> alias = new HashMap<String, String>();
    HashMap<String, String> projections = new HashMap<String, String>();
    List<Criterion> criterions = new ArrayList<Criterion>();
    projections.put("username", "username");
    projections.put("enabled", "enabled");
    Criterion criterion0 = Restrictions.ne("username", "syed");
    Criterion criterion1 = Restrictions.eq("enabled", "true");
    criterions.add(criterion0);
    criterions.add(criterion1);
    criteriaWithAlias.setAlias(alias);
    criteriaWithAlias.setCriterions(criterions);
    criteriaWithAlias.setProjections(projections);

    List<UserDTO> users = userService
            .findByCriterionWithProjection(criteriaWithAlias);
    for (UserDTO user : users) {
        org.junit.Assert.assertNull(user.getPassword());
        org.junit.Assert.assertNull(user.getId());
        org.junit.Assert.assertFalse(user.getUsername().isEmpty());
        org.junit.Assert.assertFalse(user.getEnabled().isEmpty());
    }

}

我还尝试导入跟踪器核心的spring-dao-配置在跟踪服务模块的spring-service-配置中。但是,它说,在那个时候,没找到SpringDAO-config.xml文件。

请让我知道有什么不对,我错过了什么,并提出了一个解决办法。我在各自的POM.xml中添加了每个模块的依赖项,而在父POM.xml中添加了所有模块的依赖项。

EN

回答 1

Stack Overflow用户

发布于 2014-05-06 08:17:37

我找到了解决我自己问题的办法。如果我错了就纠正我。不可能通过从另一个maven模块访问配置文件来执行JUnit测试。JUnit只用于单元测试。而不是集成测试。

依赖的maven模块配置文件将无法在类路径中用于要测试的实际maven模块。因此,我所做的就是将依赖的maven模块的配置文件复制到实际maven模块的类路径中,我需要测试这个类路径。这可能不是正确的做法。但是,我能够成功地执行JUnit测试。

在这种情况下执行Junit测试的其他方法是使用诸如MOCKITO:https://code.google.com/p/mockito/之类的工具

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23319770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档