1

JUnitParams is only passing primitive objects (String, int) but not other objects, eg.:

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

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

Only "testString" gets passed, remaining are null. Is there a workaround to pass non-primitive parameters?

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
user1810502
  • 531
  • 2
  • 7
  • 19

1 Answers1

3

Your example is missing some details. The example below works for me.

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 {
    }
}

Take a look at a blog post where I add more details if you are interested, http://thomassundberg.wordpress.com/2014/04/24/passing-non-primitive-objects-as-parameters-to-a-unit-test/

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25