从零学springboot—— springboot集成junit测试

在日常的开发中,使用junit测试是必不可少的,下来我们来学习下,在springboot中如何使用junit测试。

  • 导入测试所需要的依赖:
<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 编写test类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {StartClass.class})
@WebAppConfiguration
public class SpringbootTest {

    @Autowired
    private PersonService personService;


    @Test
    public void testJunitTest(){
        System.out.println(personService);
    }

}

值得注意的是,类上的第二个注解,在springboot较低的版本中使用的是@SpringApplicationConfiguration(classes = StartClass.class)。

此时,我们就可以编写我们的测试方法了。
在junit中除了使用@Test注解来编写测试案例,还有一些其他的注解,以下为简单介绍:

//测试类初始化方法,在启动时执行,注意,该方法为类方法(static)
@BeforeClass
//测试类的结束方法,一般为容器销毁时执行,也是类方法
@AfterClass
//每个测试方法执行前执行
@Before
//每个方法执行后执行
@After
//不执行的方法
@Ignore
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值