简体   繁体   English

junit assertEquals忽略大小写

[英]junit assertEquals ignore case

im just moving from c# -> java. 我只是从c# - > java移动。 I need to write some tests using junit. 我需要使用junit编写一些测试。 In my test i need to compare two strings to see if they match. 在我的测试中,我需要比较两个字符串,看看它们是否匹配。 So we also have the Assert.assertEquals, but this is case sensitive. 所以我们也有Assert.assertEquals,但这是区分大小写的。 How can i make it case insensitive? 我怎样才能使它不区分大小写? What i need is: 我需要的是:

"blabla".equals("BlabLA")

to return true. 返回真实。

So in C#, we used to have : 所以在C#中,我们曾经有:

public static void AreEqual (
    string expected,
    string actual,
    bool ignoreCase,
    string message
)

I was quickly going thru Junit docs, but i can't seem to find anything like this. 我很快就通过Junit文档,但我似乎无法找到这样的东西。

I find that Hamcrest provides must better assertions than the default JUnit asserts. 我发现Hamcrest必须提供比默认JUnit断言更好的断言。 Hamcrest gives MANY MANY more options and provides better messages on failure. Hamcrest提供了更多选项,并在失败时提供更好的消息。 Some basic Hamcrest matchers are built into JUnit and JUnit has the assertThat built in so this is not something totally new. 一些基本的Hamcrest匹配器构建在JUnitJUnit具有内置的assert ,所以这不是全新的。 See the hamcrest.core package in the JUnit API here . hamcrest.core的封装JUnit API 在这里 Try IsEqualIgnoringCase which would look like this. 尝试IsEqualIgnoringCase ,它看起来像这样。

assertThat(myString, IsEqualIgnoringCase.equalToIgnoringCase(expected));

With static imports this would be 使用静态导入,这将是

assertThat(myString, equalToIgnoringCase(expected));

if you want to get really fancy you would do: 如果你想得到真正的幻想,你会做:

assertThat(myString, is(equalToIgnoringCase(expected)));

One of the advantages of this is that a failure would state that expected someString but was someOtherString . 这样做的一个优点是失败会说明expected someString but was someOtherString As opposed to expected true got false when using assertTrue . 与使用assertTrueexpected true got false相反。

Use 使用

"blabla".equalsIgnoreCase("BlabLA") use for check equality ignore case

Then you can use 然后你可以使用

 assertTrue("blabla".equalsIgnoreCase("BlabLA"))

There is no direct support for this assert in JUnit (assuming you are using JUnit of course), but you could use: JUnit没有对此断言的直接支持(假设您当然使用JUnit),但您可以使用:

assertTrue("blabla".equalsIgnoreCase("BlabLA"))

It may be worth wrapping this in a separate helper method which provides a sensible failure message if they don't match (look at the docs for assertTrue to see how this could be done). 可能值得将它包装在一个单独的帮助器方法中,如果它们不匹配则提供合理的失败消息(查看assertTrue的文档以了解如何完成此操作)。

What about: 关于什么:

assertEquals("blabla","BlabLA".toLowerCase());

or 要么

assertEquals(expectedLowerCaseString,actualString.toLowerCase());

Then you can still see the difference if they are not equal. 如果它们不相等,你仍然可以看到差异。

你看过String JavaDoc了吗?

"blabla".equalsIgnoreCase("BlabLA")

你可以使用assertTrue(s1.equalsIgnoreCase(s2))

考虑AssertJ (以前称为 FEST)的断言,它是多年来我最喜欢的测试断言API:

assertThat("blabla").isEqualToIgnoringCase("BlabLA");

字符串 doc检查equalsIgnoreCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM