Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit 4.12 converts null string parameters to "null"s #1488

Open
MGhareeb32 opened this issue Oct 25, 2017 · 12 comments
Open

JUnit 4.12 converts null string parameters to "null"s #1488

MGhareeb32 opened this issue Oct 25, 2017 · 12 comments

Comments

@MGhareeb32
Copy link

When running the following test with junitLibVersion = "4.12":

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class TestNull {

    private final String mInput;

    @Parameters
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { {null} });
    }

    public TestNull(String input) {
        mInput = input;
    }
    @Test
    public void test() {
        assertEquals(null, mInput);
    }
}

I get the following assertion failure:

java.lang.AssertionError: expected: null<null> but was: java.lang.String<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.myapp.android.regression.TestNull.test(TestNull.java:27)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
@stefanbirkner
Copy link
Contributor

I created a project stefanbirkner/junit4-issue-1488 for verifying the issue.

I cannot reproduce the failure with Java 8. @MGhareeb32 Can you please verify that the example project is failing for you, too.

@kcooney
Copy link
Member

kcooney commented Oct 25, 2017

Thanks, @stefanbirkner ; I was going to do the same thing :-)

I noticed Android classes on the call stack. @MGhareeb32 does this happen for projects that don't depend on Android?

@MGhareeb32
Copy link
Author

MGhareeb32 commented Oct 26, 2017

Thanks @stefanbirkner and @kcooney.
Pulling the project and trying to reproduce...

@MGhareeb32
Copy link
Author

I'm afraid I'm not able to reproduce outside of the Android setup I have. I tried to change @stefanbirkner's repo to use the same dependencies my Android repo has, but no luck.

@stefanbirkner
Copy link
Contributor

@MGhareeb32 Can you change my repo so that it has an Android setup?

@MGhareeb32
Copy link
Author

Will get back to you later today :)

@kcooney
Copy link
Member

kcooney commented Oct 27, 2017

If you are able to launch your tests in a debugger, I suggest doing that.

@junit-team junit-team deleted a comment from viktor22833 Nov 27, 2017
@kcooney kcooney closed this as completed Feb 24, 2018
@npmccallum
Copy link

I just ran into this today. It was very painful.

@kcooney
Copy link
Member

kcooney commented Jun 21, 2018

@npmccallum we only closed this because no one could reproduce it with vanilla JUnit 4.12. Could you provide a reproducible test case?

@dabrosch
Copy link

dabrosch commented Nov 19, 2019

I also just ran into this today, and it specifically occurs when I run my JUnit Android Espresso test on an Emulator running API 23, but not with 26 or greater.

Building on Java 8, with Android SDK 28.

@kcooney
Copy link
Member

kcooney commented Nov 19, 2019

@dabrosch my guess is that this is Android-specific, which is why the JUnit team hasn't been able to reproduce it. Could you use a debugger to try to figure out why you are getting this failure message?

@kcooney kcooney reopened this Nov 19, 2019
@dabrosch
Copy link

dabrosch commented Dec 3, 2019

I can confirm that the result of Arrays.asList(new Object[][] { {null} } is the exact same when executing on Android API 23 or 26, therefore it must be in the Parameter assignment to a string that is coercing it into a String "null."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants