1

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

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

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

只有"testString"通过,剩下的是null。是否有解决方法来传递非原始参数?

4

1 回答 1

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 -测试/

于 2013-12-28T14:34:51.460 回答