标签: junitparams

junitparameter异常方法应该没有参数

通过使用JUnitParameter来测试方法我有一个例外.我的代码类似于JUnitParameter上的大量示例:

    private Object parametersForTestSetDistanceFormated() {
    return new Object[][]{    
        {100,   "_1,__ km"}, 
        {100,   "1_,__ km"}, 
        {1100,  "11,__ km"}, 
        {110,   "1_,1_ km"}, 
        {111,   "1_,11 km"}};
}
/**
 * Test of setDistanceFormated method, of class ClientFormated.
 */
@Test
@Parameters
public void testSetDistanceFormated(final int exptDist, final String  distFormated) {
    System.out.println("setDistanceFormated");
    ClientFormated instance = new ClientFormated();             

    instance.setDistanceFormated(distFormated);
    assertEquals(exptDist, (long) instance.getDistance());
}
Run Code Online (Sandbox Code Playgroud)

然后我获得以下异常:

java.lang.RuntimeException: java.lang.Exception: Method testSetDistanceFormated should have no parameters
    at junitparams.JUnitParamsRunner.verifyMethodCanBeRunByStandardRunner(JUnitParamsRunner.java:429)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:420)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:386)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at …
Run Code Online (Sandbox Code Playgroud)

java exception junitparams

8
推荐指数
3
解决办法
2万
查看次数

在JunitParams中使用"逗号"或"管道"

我正在尝试使用JunitParams来参数化我的测试.但我的主要问题是参数是带有特殊字符,波形符或管道的字符串.

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

@RunWith(JUnitParamsRunner.class)
public class TheClassTest {

    @Rule
    public ExpectedException exception = ExpectedException.none();

     @Test
     @Parameters({"AA~BBB"})
     public void shouldTestThemethod(String value) throws Exception {
        exception.expect(TheException.class);

        TheClass.theMethod(value);     
        // Throw an exception if value like "A|B" or "A~B",
        // ie if value contain a ~ or a |
    }
}
Run Code Online (Sandbox Code Playgroud)

随着代字号,我没有问题.但是有了管道,我有一个例外:

java.lang.IllegalArgumentException: wrong number of arguments
Run Code Online (Sandbox Code Playgroud)

管道(以逗号形式)用作参数的分隔符.

我有什么方法可以设置不同的分隔符吗?或者这是JunitParams的限制?

java junit pipe comma junitparams

7
推荐指数
3
解决办法
1461
查看次数

JUnitParams与Mockito

这可能是XY问题,但我想问:

我正在使用JUnitParams来对不同的对象运行测试方法10次。问题在于注入不起作用(@Mock@InjectMocks)。我可以用

PersonService personService = mock(PersonServiceImpl.class)
Run Code Online (Sandbox Code Playgroud)

嘲笑PersonService@Test方法。但是,如何模拟带有注释的“主要”对象@InjectMock呢?也许是不可能的,并且JUnitParams仅针对它们在其站点上举例说明的简单测试用例创建。

如果我不能使用JUnitParams,我该怎么办?我试过了

@RunWith(value = Parameterized.class)
Run Code Online (Sandbox Code Playgroud)

但这也不起作用..我想..

是)我有的:

@RunWith(JUnitParamsRunner.class)
public class SkapaKundMasterDataTest {

    @InjectMocks
    private SkapaKundMasterData batch;

    @Mock
    private PersonService personService;

    @Mock
    private UtbetalningService utbetalningService;

    @Mock
    private Användare user;

    public Collection<PersonA[]> paramz() {

        List<PersonA[]> params = new ArrayList<>();
        params.add(new PersonA[] {new PersonA(new PersonId(1111111111111L), new Personnummer(195001019999L), Datum.skapaDatum(1950, 1, 1), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(2222222222222L), new Personnummer(195102029999L), …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing mockito junitparams

6
推荐指数
1
解决办法
1016
查看次数

JUnitParams不使用String数组

考虑这个测试类,使用JUnit 4和JUnitParams:

import static junitparams.JUnitParamsRunner.$;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

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

@RunWith(JUnitParamsRunner.class)
public class JUnitParamsExample {

    private int[] getIntArray() {
        int array[] = new int[2];
        array[0] = 1;
        array[1] = 2;
        return array;
    }

    public Object getInts() {
        return $($(getIntArray()));
    }

    @Parameters(method = "getInts")
    @Test
    public void testIntArray(int... values) {
        //
    }

    private String[] getStringArray() {
        String array[] = new String[2];
        array[0] = "a";
        array[1] = "b";
        return array;
    }

    public Object getStrings() {
        return $($(getStringArray()));
    }

    @Parameters(method …
Run Code Online (Sandbox Code Playgroud)

java junit parameterized-unit-test junitparams

5
推荐指数
1
解决办法
5253
查看次数

Junit使用文件输入参数化测试

我有一个关于使用参数化测试来查询我的API单元测试的查询.而不是建立一个类似的arraylist

Arrays.asList(new Object[]{
   {1},{2},{3}
});
Run Code Online (Sandbox Code Playgroud)

我想逐个读取文件中的行并将它们填入数组中.这样一切都会被推广.任何人都可以用示例建议我这样的方法吗?

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

编辑:Duncan提出的代码

@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 …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing junitparams

4
推荐指数
1
解决办法
1万
查看次数

如何在Spring Boot Starter测试中应用两个@RunWith

我正在使用Spring Boot Starter Test编写JUnit测试用例。我很想使用JunitParamrunner,它可以方便地传递文件以进行参数化测试。基本上,它逐行从文件中读取数据,并且每行都调用一个测试用例。问题是同时使用SpringJUnit4ClassRunner和JUnitParamsRunner都需要传递@RunWith。我不知道该怎么做。谁能提供一些线索。

java junit4 parameterized-tests spring-boot junitparams

3
推荐指数
1
解决办法
3043
查看次数

使用 JUnitParams 的 @FileParameters 时如何为参数赋予空值

我尝试使用 JUnitParamsRunner 的 FileParameters 注释。我不能给一个变量 null。代码和测试文件如下。

@RunWith(JUnitParamsRunner.class)
public class PasswordCheckerFileParameterizedTest {


    @Test
    @FileParameters("src/test/resources/testScenarios.csv")
    public void checkPasswordIsValid_checkMultipleCase(String password,boolean expectedResult){
        PasswordChecker passwordChecker = new PasswordChecker();
        assertEquals(expectedResult,passwordChecker.checkPasswordIsValid(password));
    }
}
Run Code Online (Sandbox Code Playgroud)

测试场景.csv

,false
sD1.,false
ssfdsdfsdf234.,false
SEWERWER234.,false
ssfdsdfsdSDFSDF.,false
ssfdsdfsdSDFSDF3234,false
ssfdsdfsdSDFSDF23.,true
Run Code Online (Sandbox Code Playgroud)

java junit junitparams

3
推荐指数
2
解决办法
3052
查看次数