错误 java.lang.AssertionError: expected: null<null> but was: java.lang.String<null> 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34223696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:25:55  来源:igfitidea点击:

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

javaspringhibernatejunit

提问by Alessandro Da Rugna

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

我的 Junit 4.12 测试代码中有这个奇怪的问题。该应用程序使用 Spring Framework 4.1.6 和 Hibernate 4。当比较来自不同数据库的两个 bean 时,我收到此错误

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 SpringJUnit4ClassRunnerand looks similar to this

我的测试类运行SpringJUnit4ClassRunner并看起来与此类似

@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:

编辑:我所指的 bean 是一个简单的 POJO,您可以想象它如下所示:

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

我让他们使用 Hibernate

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.

为了解决最终投票:我不确定所有这些细节是否有帮助,问题是关于我得到的奇怪的 AssertError 而不是我如何得到豆子。我在 SO 或 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.

edit2:为了进一步澄清,POJO 本身是完全相同的 Java 类,我使用了两个休眠映射文件。唯一的区别是catalog="this",并catalog="that"在映射。我使用两个不同的 sessionfactories,因为数据存储在不同的模式(又名目录)中,同一个 MySQL 实例。

采纳答案by Alessandro Da Rugna

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

经过更多的挖掘,我发现这些豆子并不相同。常规 DAO在 Thing 上将字符串设置为null,而测试数据 DAO在 Thing 上设置null。失败的断言是

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

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

我很高兴,失败的话,价值不同的。详细信息虽然很神秘。

回答by Iker Obregon Reigosa

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

这看起来像是 JUnit 4.12 版本的问题。github 上的 JUnit 出现了一个问题,现已修复。因此,解决方法是降级到 4.11 或采用最新版本的 JUnit。

回答by Josh

I had a similar problem and solved it by wrapping the values with String.valueOf(Your Value Here) e.g. String.valueOf("null")

我有一个类似的问题,并通过用 String.valueOf(Your Value Here) 例如 String.valueOf("null") 包装值来解决它

回答by Rock Lee

I found the correct answerto be confusing and led me to think it was some complicated difference between the way GreenDAO stores String yourString = nulland `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:

我发现正确答案令人困惑,并让我认为这是 GreenDAO 存储方式String yourString = null和“null”之间的一些复杂差异,让我觉得我需要将 JUnit 升级到版本 5,但幸运的是我发现了真正的含义答案是:

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

此错误仅表示您正在比较null和 string "null",它们不相等。就我而言,"null"当我打算存储null.

回答by PRAFUL ANAND

Please Try below TestMethod

请尝试以下测试方法

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