JUnitParams 仅传递原始对象(String、int)而不传递其他对象,例如:
@Test
@Parameters
testMethod(String sample, MyObj myobj, MyObj myobj)
{}
private Object[] parametersForTestMethod()
{
return $($("testString", myobj, myotherobj));
}
只有 "testString"
通过,其余为 null
。是否有传递非原始参数的解决方法?
最佳答案
您的示例缺少一些细节。下面的示例对我有用。
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/
关于java - JUnitParams 将非原始对象传递给测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411021/