同时使用Junit4的@Parameterized参数化测试和Spring容器

同时使用Junit4的@Parameterized参数化测试和Spring容器整合


之剑 2016.4.30

整合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);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • TestNG系列:TestNG和Junit4的参数化测试对比TestNG运行指定测试套件TestNG整合Repor...
    测试你个头阅读 2,316评论 0 2
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 从明天开始 要比鸡起得早 比狗睡得晚 一张大嘴喷喷喷 苦口婆心 、、 从明天开始 要陪着熊孩子 唱歌跳舞读书学习 ...
    0桂花香0阅读 361评论 2 7
  • 当我醒来,恍恍若有所失 梦中总有一个人 我深深爱着 给我温暖和归属感 睁开眼睛却模糊了她的音容 努力记忆却怎么也想...
    星辰如洒揽凉风阅读 191评论 0 3