JUnitParams将非原始对象传递给测试方法

时间:2013-12-05 21:22:55

标签: java unit-testing junit junitparams

JUnitParams只传递原始对象(String,int),但不传递其他对象,例如:

@Test
@Parameters
testMethod(String sample, MyObj myobj, MyObj myobj)
{}

private Object[] parametersForTestMethod()
{
   return $($("testString", myobj, myotherobj));
}

仅传递"testString",其余为null。是否有传递非原始参数的解决方法?

1 个答案:

答案 0 :(得分:3)

您的示例缺少一些细节。以下示例适用于我。

package se.thinkcode;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junitparams.JUnitParamsRunner.$;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

@RunWith(JUnitParamsRunner.class)
public class NonPrimitiveObjectsTest {

    @Test
    @Parameters(method = "parametersForTestMethod")
    public void shouldReceiveNonPrimitiveParameters(String sample, MyObj myobj) {
        assertFalse("The sample string should not be empty", sample.isEmpty());
        assertNotNull("The non primitive parameter should not be null", myobj);
    }

    @SuppressWarnings("unused")
    private Object[] parametersForTestMethod() {
        return $($("testString", new MyObj()));
    }

    class MyObj {
    }
}

如果您有兴趣,可以查看我发布的博客文章,http://thomassundberg.wordpress.com/2014/04/24/passing-non-primitive-objects-as-parameters-to-a-unit-test/