'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.
Solution 1:[1]
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.
Solution 2:[2]
I had a similar problem and solved it by wrapping the values with String.valueOf(Your Value Here) e.g. String.valueOf("null")
Solution 3:[3]
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.
Solution 4:[4]
Please Try below TestMethod
@Test
public void testNull() {
assertEquals(null, "");
}
Solution 5:[5]
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
.
Solution 6:[6]
I was getting error because I was using :
@Test
fun trimRightNullIsSetToNull() {
assertThat(trimRight(null), equalTo(nullValue()))
}
Removing equalTo and directly comparing them both solved the error :
@Test
fun trimRightNullIsSetToNull() {
assertThat(trimRight(null), nullValue())
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alessandro Da Rugna |
Solution 2 | Josh |
Solution 3 | |
Solution 4 | PRAFUL ANAND |
Solution 5 | Rock Lee |
Solution 6 | oyeraghib |