zoukankan      html  css  js  c++  java
  • 同时使用Junit4的@Parameterized参数化测试和Spring容器

    转载:http://www.jianshu.com/p/d191fe54915f

    整合Spring容器

    @SpringApplicationConfiguration(classes = Application.class)
    @WebAppConfiguration
    public class TestBase {
       @Autowired
       protected TedaCaseService tedaCaseService;
    
       private TestContextManager testContextManager;
    
       @Before
       public void setUpContext() throws Exception {
          this.testContextManager = new TestContextManager(getClass());
          this.testContextManager.prepareTestInstance(this);
       }
    
    }

    Junit4的参数化测试

    @RunWith(Parameterized.class):

    package meeting.httpapi.test;
    
    import com.teda.model.TedaCaseVo;
    import meeting.TestBase;
    import meeting.tool.SwordHttp;
    import meeting.tool.TedaTool;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    /**
     * @author jack
     */
    @RunWith(Parameterized.class)
    public class MeetingHttpApiTest extends TestBase {
    
        @Test
        public void testMeetingHttpApi() {
            TedaCaseVo tedaCaseVo = tedaCaseService.getTedaCase(tedaCaseId);
            String url = tedaCaseVo.getParamJsonStr();
            TedaTool.record(tedaCaseVo, SwordHttp.get(url), tedaCaseService);
        }
    
        @Parameters
        public static Collection data() {
            return Arrays.asList(new Object[][]{{22L}, {23L}, {24L},{25L}, {26L}, {27L}});
        }
    
        public MeetingHttpApiTest(Long tedaCaseId) {
            this.tedaCaseId = tedaCaseId;
        }
    
        protected Long tedaCaseId = 1L;
    
    }
    
    
    
    
    package meeting.tool;
    
    import com.teda.model.TedaCaseVo;
    import com.teda.service.TedaCaseService;
    
    import static org.junit.Assert.assertTrue;
    
    /**
     * @author jack
     */
    public class TedaTool {
        /**
         * @param tedaCaseVo
         * @param actualOutput
         */
        public static void record(TedaCaseVo tedaCaseVo, String actualOutput, TedaCaseService tedaCaseService) {
            print("tedaCaseVo===" + tedaCaseVo);
            print("actualOutput===" + actualOutput);
            tedaCaseVo.setActualOutput(actualOutput);
            String expectOutput = tedaCaseVo.getExpectOutput();
            boolean contains = actualOutput.contains(expectOutput);
            int state = contains ? 1 : 0;
            tedaCaseVo.setState(state);
            tedaCaseService.updateTedaCase(tedaCaseVo);
            assertTrue(contains);
        }
    
    
        public static void print(Object o) {
            System.out.println(o);
        }
    
        public static void clearData(TedaCaseVo tedaCaseVo, TedaCaseService tedaCaseService) {
            tedaCaseService.clearData(tedaCaseVo);
        }
    
    }



    文/东海陈光剑(简书作者)
    原文链接:http://www.jianshu.com/p/d191fe54915f
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    洛谷 P1567 统计天数【最长上升子序列/断则归一】
    洛谷 P3742 umi的函数【构造】
    洛谷 P1036 选数【背包型DFS/选or不选】
    nyoj zb的生日【背包型DFS/选or不选】
    POJ 3628 Bookshelf 2【背包型DFS/选or不选】
    【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
    洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
    洛谷 P1004 方格取数 【多线程DP/四维DP/】
    Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
    Codeforces Round #449 (Div. 2) A. Scarborough Fair【多次区间修改字符串】
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6247601.html
Copyright © 2011-2022 走看看