@After方法

时间:2016-09-24 10:00:28

标签: junit

我试图在每个测试方法之后收集一些信息,并希望在测试类完成后分析收集的信息。所以,我有一个私有成员变量,我希望在每个测试方法完成后添加一个列表。但是,在一天结束时,成员变量始终保持为空。

注意:我的测试类实现了Callable接口。

这是我的代码段:

{

private List<String statisticsCollector;

private JUnitCore core = null;

private int x = 0;

public MyLoadTest() {
    this.core = new JUnitCore();
    this.statisticsCollector = new ArrayList<String>();
}

@Override
public List<String> call() {
    log.info("Starting a new thread of execution with Thread# -" + Thread.currentThread().getName());
    core.run(this.getClass());
    return getStatisticsCollector(); // this is always returing a list of size 0
}

@After
public void gatherSomeStatistics() {
    x = x+1;
    String sb = new String("Currently executing ----" + x);
    log.info("Currently executing ----" + x);
    addToStatisticsCollector(sb);
}

@Test
@FileParameters(value = "classpath:folder/testB.json", mapper = MyMapper.class)
public void testB(MarsTestDefinition testDefinition) {
    runTests(testDefinition);
}

@Test
@FileParameters(value = "classpath:folder/testA.json", mapper = MyMapper.class)
public void testA(MyDefinition testDefinition) {
    runTests(testDefinition);
}

public List<String> getStatisticsCollector() {
    return this.statisticsCollector;
}

public void addToStatisticsCollector(String sb) {
    this.statisticsCollector.add(sb);
}

}

那么,为什么它总是被重置,即使我在@After注释方法中附加到列表中? 任何帮助将受到高度赞赏。感谢

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码,是否有效?

private static List<String> statisticsCollector = new ArrayList<String>();

private JUnitCore core = null;

private int x = 0;

public MyLoadTest() {
    this.core = new JUnitCore();
}

public List<String> getStatisticsCollector() {
    return statisticsCollector;
}