JUnitParams, передающий непримитивные объекты в тестовый метод

JUnitParams передает только примитивные объекты (String, int), но не другие объекты, например:

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

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

Только "testString" проходит, остальные null, Есть ли обходной путь для передачи не примитивных параметров?

1 ответ

В вашем примере отсутствуют некоторые детали. Пример ниже работает для меня.

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/

Другие вопросы по тегам