zoukankan      html  css  js  c++  java
  • springboot-13-junitTest

    junitTest, 提喜欢用的一个方法, 在测试代码时非常好用

    1, 添加maven依赖

            <!-- 加入spring-test依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

    2, 编写测试代码

    package com.iwhere.test;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.iwhere.main.service.DemoService;
    
    /**
     * springboot 使用junitTest
     * @author wenbronk
     * @time 2017年3月17日  下午5:27:01  2017
     */
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
    public class SpringTestJunit {
    
        @Autowired
        private DemoService demoService;
    
        @Test
        public void exampleTest() {
            System.out.println("success");
        }
        
    }

    注解介绍: 

    Junit基本注解介绍
    //在所有测试方法前执行一次,一般在其中写上整体初始化的代码 
    @BeforeClass
     
     
    //在所有测试方法后执行一次,一般在其中写上销毁和释放资源的代码 
    @AfterClass
     
     
    //在每个测试方法前执行,一般用来初始化方法(比如我们在测试别的方法时,类中与其他测试方法共享的值已经被改变,为了保证测试结果的有效性,我们会在@Before注解的方法中重置数据) 
    @Before
     
     
    //在每个测试方法后执行,在方法执行完成后要做的事情 
    @After
     
     
    // 测试方法执行超过1000毫秒后算超时,测试将失败 
    @Test(timeout = 1000)
     
     
    // 测试方法期望得到的异常类,如果方法执行没有抛出指定的异常,则测试失败 
    @Test(expected = Exception.class)
     
     
    // 执行测试时将忽略掉此方法,如果用于修饰类,则忽略整个类 
    @Ignore(“not ready yet”) 
     
    
    @Test
     
     
    @RunWith 
    
    在JUnit中有很多个Runner,他们负责调用你的测试代码,每一个Runner都有各自的特殊功能,你要根据需要选择不同的Runner来运行你的测试代码。 
  • 相关阅读:
    Windows2003 Webshell默认权限
    Windows安装Centos7双系统后Windows启动项消失
    GCC编译流程及常用编辑命令
    swoole的websockte例子
    PhpStorm 增加Swoole智能提示
    Centos7/RHEL 7 配置静态路由
    webpack介绍和使用
    Webpack中的sourcemap以及如何在生产和开发环境中合理的设置
    什么是 PWA
    php实现excel单元格合并,字体加粗居中等操作
  • 原文地址:https://www.cnblogs.com/wenbronk/p/6567568.html
Copyright © 2011-2022 走看看