Spring 单元测试配置

Spring 单元测试配置

增加相关依赖

 <dependencies>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
     </dependency>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <scope>test</scope>
     </dependency>
 </dependencies>

增加扫描包目录

@Configuration
@ComponentScan("pr.iceworld.fernando.demospringtest")
public class ConfigComponentScan {
}

定义相关service或者component

// 此类定义组件注解
@Service
public class ServiceA {
    public void doAction() {
        System.out.println("serviceA.doAction()");
    }
}
// 此类没有定义组件注解
public class ServiceB {
    public void doAction() {
        System.out.println("serviceB.doAction()");
    }
}

增加测试额外配置文件 如 spring-conf.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       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
        ">
    <bean id="serviceB" class="pr.iceworld.fernando.demospringtest.service.ServiceB"/>
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
// 增加单元测试额外配置文件
@ContextConfiguration(locations = {"classpath:spring-conf.xml"})
public class TestBase {

    @Autowired
    ServiceB serviceB;

    @Test
    public void doAction() {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ConfigComponentScan.class);
        ServiceA serviceA = annotationConfigApplicationContext.getBean(ServiceA.class);
        serviceA.doAction();
        serviceB.doAction();
    }
}
serviceA.doAction()
serviceB.doAction()

那有什么方法可以让component scan 与 xml 配置在测试中同时加载,而不需要其中一个从ApplicationContext 中显示获取,使用 @ContextHierarchy

package org.springframework.test.context;
// ...
/**
 * ...
 * This annotation may be used as a meta-annotation to create custom composed annotations.
 * ...
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ContextHierarchy {
	/**
	 * A list of {@link ContextConfiguration @ContextConfiguration} instances,
	 * each of which defines a level in the context hierarchy.
	 * <p>If you need to merge or override the configuration for a given level
	 * of the context hierarchy within a test class hierarchy, you must explicitly
	 * name that level by supplying the same value to the {@link ContextConfiguration#name
	 * name} attribute in {@code @ContextConfiguration} at each level in the
	 * class hierarchy. See the class-level Javadoc for examples.
	 */
	ContextConfiguration[] value();
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(
        value = {
                @ContextConfiguration(
                           classes = ConfigComponentScan.class
                ),
                @ContextConfiguration(
                        locations = {"classpath:spring-conf.xml"}
                )
        }
)
public class TestBase {

    @Autowired
    ServiceB serviceB;
    @Autowired
    ServiceA serviceA;

    @Test
    public void doAction() {
        serviceA.doAction();
        serviceB.doAction();
    }
}

正常输出结果

serviceA.doAction()
serviceB.doAction()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值