4

我有一个关于使用参数化测试对我的 API 进行单元测试的问题。现在而不是像构建一个数组列表

Arrays.asList(new Object[]{
   {1},{2},{3}
});

我想一一读取文件中的行,将它们拆分空间并将它们填充到数组中。这样一来,一切都将被概括。任何人都可以通过示例向我建议任何这样的方法吗?

还有一种方法可以在不将各种参数声明为私有成员并在测试单元的构造函数中初始化它们的情况下进行测试?

编辑:邓肯要求的代码

@RunWith(Parameterized.class)
public class JunitTest2 {

    SqlSession session;
    Integer num;
    Boolean expectedResult;
    static BufferedInputStream buffer = null;

    public JunitTest2(Integer num, Boolean expected){
        this.num = num;
        this.expectedResult = expected;
    }

    @Before
    public void setup() throws IOException{
        session = SessionUtil.getSqlSessionFactory(0).openSession();
        SessionUtil.setSqlSession(session);
        buffer = new BufferedInputStream(getClass().getResourceAsStream("input.txt"));
        System.out.println("SETUP!");
    }

    @Test
    public void test() {
        assertEquals(expectedResult, num > 0);
        System.out.println("TESTED!");
    }

    @Parameterized.Parameters
    public static Collection getNum() throws IOException{
        //I want my code to read input.txt line by line and feed the input in an arraylist so that it returns an equivalent of the code below

        return Arrays.asList(new Object[][]{
            {2, true},
            {3, true},
            {-1, false}
        });
    }
    @After
    public void tearDown() throws IOException{
        session.close();
        buffer.close();
        System.out.println("TEARDOWN!");
    }
}

我的 input.txt 也将如下所示:

2 true
3 true
-1 false
4

3 回答 3

14
@RunWith(JUnitParamsRunner.class)
public class FileParamsTest {

    @Test
    @FileParameters("src/test/resources/test.csv")
    public void loadParamsFromFileWithIdentityMapper(int age, String name) {
        assertTrue(age > 0);
    }

}

JUnitParams has support for loading the data from a CSV file.

CSV File will contain

1,true
2,false
于 2014-01-28T09:58:45.897 回答
3

看看junitparams项目,尤其是这个例子。它将向您展示如何使用 CSV 文件进行参数输入。这是一个简短的示例:

我的 test.csv 文件:

1, one
2, two
3, three

我的测试:

package com.stackoverflow.sourabh.example;

import static org.junit.Assert.assertTrue;
import junitparams.FileParameters;
import junitparams.JUnitParamsRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)
public class FileReadingParameterizedTest {

    @Test
    @FileParameters("src/test/resources/test.csv")
    public void testWithCSV(int number, String name) {
        assertTrue(name + " is not at least two", number >= 2);
    }

}

显然第一个测试将失败,产生错误消息one is not at least two

于 2014-01-28T09:39:17.803 回答
1

在 Spring-boot Java 框架中,可以Value方便地在类中使用注解,

@Component
public class MyRunner implements CommandLineRunner {

@Value("classpath:thermopylae.txt") //Annotation
private Resource res; // res will hold that value the `txt` player

@Autowired
private CountWords countWords;

@Override
public void run(String... args) throws Exception {

    Map<String, Integer> words =  countWords.getWordsCount(res);

    for (String key : words.keySet()) {

        System.out.println(key + ": " + words.get(key));
       }
   }
}

资源

于 2020-06-27T12:58:05.553 回答