Spring5 Test测试框架

Spring5 的测试框架本身就是依赖Junit5的,有部分用法需要了解Junit5才能够理解,好比如

@RunWith

这个注解,这个注解是Junit4的注解就是一个运行器

@RunWith(JUnit4.class)就是指用JUnit4来运行
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
@RunWith(Suite.class)的话就是一套测试集合,
@RunWith(SpringRunner.class)
这个注解是首要而且是必须有的,至于选择RunWith那种套件,由自己选择 其中SpringRunner这个类是SpringJunit4ClassRunner
的一个子类,可以理解为简称。

一般在Spring测试框架中使用有四种形式可以使用

分别为注解性配置文件与非注解型配置,再之的分类是普通的Spring应用与Web形式的Spring应用

1.首先是非注解型(xml)配置型的普通Spring应用

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
@WebAppConfiguration
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

他有一个简写的方式就是

@RunWith(SpringRunner.class)
@SpringJUnitConfig(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

2.其次是非注解型(xml)配置型的SpringMVC应用

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
@WebAppConfiguration
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

相应的简写方式是

@RunWith(SpringRunner.class)
@SpringJUnitConfig(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

对应,如果采用的是Java 配置文件作为Spring配置文件的话,基本上没有大的改变,只是将注解配置的

locations修改为class,并且配置为xxx.class就可以。

关于BootstrapWith

这个注解要求注入类型为TestContextBootstrapper

Class<? extends TestContextBootstrapper> value() default TestContextBootstrapper.class;
从源代码可以看得出来

可以通过配置DefaultTestContextBootstrapper 或者WebTestContextBootstrapper来决定测试的是Web应用还是普通Spring应用,此外也可以省去@WebAppConfiguration这样的配置信息

实例代码如下

普通Spring应用

@BootstrapWith(DefaultTestContextBootstrapper.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

SpringMvc应用

@BootstrapWith(WebTestContextBootstrapper.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/dispatcher-config.xml")
public class SpringTest {

    @Test
    public void tester(){
        System.out.println("Hello");
    }
}

@Profile注解用于设置在@Bean上面,用于声明,这个@Bean在@ActiveProfiles("devs"),的使用才会执行实例化,其实互相配套使用的

而且里面的值相同才会启用@ActiveProfiles在类级别上使用,也可以通过设置spring.profiles.active这个值,命令行启动的使用

java xx.jar -Dspring.profiles.active=dev,abc来指定启用的profile从而在不同的环境下启用不同的配置

 
@ActiveProfiles("dev")
@Profile("dev")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值