同时使用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);
    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏前端儿

VF

Vasya is the beginning mathematician. He decided to make an important contributi...

881
来自专栏小樱的经验随笔

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3...

2756
来自专栏陈树义

6.Redis常用命令:Set

  在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加、删除或判断某一元素是否存在等操作。需...

3287
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

1973
来自专栏一个会写诗的程序员的博客

java.lang.reflect.InaccessibleObjectException: Unable to make

java.lang.reflect.InaccessibleObjectException: Unable to make ...

752
来自专栏算法修养

FZU 2150 Fire Game(BFS)

Problem 2150 Fire Game Accept: 1302    Submit: 4569 Time Limit: 1000 mSec    M...

3254
来自专栏算法修养

POJ-1125 Stockbroker Grapevine

Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Sub...

3114
来自专栏码匠的流水账

聊聊flink LocalEnvironment的execute方法

flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/DataSet.java

1873
来自专栏java、Spring、技术分享

深入分析Spring MVC中RequestBody与ResponseBody

  在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换。在Sprin...

2581
来自专栏码匠的流水账

聊聊storm的GraphiteStormReporter

storm-core-1.2.2-sources.jar!/org/apache/storm/metrics2/reporters/GraphiteStormR...

1391

扫码关注云+社区

领取腾讯云代金券