Skip to content
Advertisement

Error java.lang.AssertionError: expected: null but was: java.lang.String 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.

Advertisement

Answer

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.

Advertisement