Skip to content

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.failNotEquals(
    at org.junit.Assert.assertEquals(
    at org.junit.Assert.assertEquals(
    at my.test.TestClass.method(

What does it mean? How to resolve it?

My test class runs with SpringJUnit4ClassRunner and looks similar to this

public class TestMdtTechnicalGridGeneration extends AbstractJUnit4SpringContextTests {

    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

public void testNull() {
    assertEquals(null, "null");

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

3 People found this is helpful