简体   繁体   中英

Error java.lang.AssertionError: expected: null<null> but was: java.lang.String<null> what does it mean?

I have this strange issue in my Junit 4.12 test code. The application uses Spring Framework 4.1.6 and Hibernate 4. When comparing two beans coming from different databases I get this error

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 my.test.TestClass.method(TestClass.java:105)

What does it mean? How to resolve it?

My test class runs with SpringJUnit4ClassRunner and looks similar to this

@ContextConfiguration(
    {
        "classpath:beans-test.xml"
    }
)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMdtTechnicalGridGeneration extends AbstractJUnit4SpringContextTests {

    @Test
    public void method() {
            assertEquals(bean1.getThing(), bean2.getThing());
    }
}

edit : the bean I'm referring to is a simple POJO, you can imagine it look like the following:

public class Thing {
    private String thing;

    public void setThing(String thing) {
        this.thing = thing;
    }

    public String getThing() {
        return thing;
    }
}

I get them using Hibernate along the line of

SessionFactory mySF = (SessionFactory) applicationContext.getBean("mySessionFactory");
Query query = mySF.openSession().createQuery("FROM Thing WHERE code = '" + code + "'");
List<Thing> listThing = return query.list();
bean1 = listThing.get(0);

To address the close vote : I'm not sure if all these details may help, the question is about the strange AssertError I get rather than how I get the beans. I can't find help here in SO nor Google.

edit2 : to further clarify, the POJO itself is the exact same Java class, I use two hibernate mappings files. The only difference is catalog="this" and catalog="that" in the mapping. I use two different sessionfactories because data is stored in different schemas (aka catalog), same MySQL instance.

After some more digging I've discovered that the beans are not identical. The regular DAO sets the string with value null on Thing, while test data DAO sets null on Thing. The failing assert is

@Test
public void testNull() {
    assertEquals(null, "null");
}

I'm happy that it fails, values are different. The detail message is quite cryptical though.

This looks like a problem with the 4.12 version of JUnit. An issue was opened with JUnit on github and has now been fixed. Therefore the workaround is to downgrade to 4.11 or take the latest build of JUnit.

我遇到了类似的问题并通过使用String.valueOf(Your Value Here)包装值来解决它,例如String.valueOf(“null”)

I found the correct answer to be confusing and led me to think it was some complicated difference between the way GreenDAO stores String yourString = null and `null", making me think I need to upgrade JUnit to version 5, but luckily I found out the true meaning of the answer, which is:

This error simply means that you are comparing null and the string "null" , which are not equal. In my case, I was storing "null" in GreenDAO when I meant to store null .

Please Try below TestMethod

@Test
public void testNull() {
assertEquals(null, "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM