测试SimpledateFormat时发生JUnit AssertionError [英] JUnit AssertionError when testing SimpledateFormat

查看:81
本文介绍了测试SimpledateFormat时发生JUnit AssertionError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JUnit 4.12和AndroidStudio,我正在尝试测试以下方法,其中结果似乎匹配,但我不知道为什么我得到了这样的结果 java.lang.AssertionError

Using JUnit 4.12 and AndroidStudio I am trying to test below method Where results seems to be matching I don't know why as a reesult I am getting java.lang.AssertionError

被测方法:

public String dateNow() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_hh-mm-ss");
    String date = sdf.format(new Date());
    return date;
}

我的测试课:

private Addons mAddons;

@Before
public void setUp() throws Exception{
    mAddons = new Addons();
}

@Test
public void testDateNow() throws Exception {

    final String reality = mAddons.dateNow().substring(0,10);
    final String expected = "09-08-2017";
    assertSame("testDateNow() :", expected, reality);
}

控制台输出:

java.lang.AssertionError: testDateNow() : expected same:<09-08-2017> was not:<09-08-2017>

Expected :09-08-2017
Actual   :09-08-2017

我不知道为什么它失败了.有人会这样请国王向我指出正确的方向吗?

I don't know why it is failing. Anyone would bee so king to point me into right direction please?

推荐答案

assertSame检查两个参数是否为同一对象.他们不是.

assertSame checks if the two parameters are the same object. And they aren't.

如果要检查它们是否具有相同的内容,则应改用assertEquals.

If you want to check if they have the same content, you should use assertEquals instead.

这篇关于测试SimpledateFormat时发生JUnit AssertionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆