I am trying to compare 2 String in assertEquals
using JUnit 4
, Java 8
, to check decryption function, so:
String data = "veryLongEncryptedString.....";
String value = DecUtils.decryptToken(data, null); //returns String
assertEquals("Here User name: ", "encrypt expected value", value);
The values are equal but the console shows one of them quoted:
org.junit.ComparisonFailure: Here is test for Addition Result: expected:<[xxx]> but was:<["xxx"]>
How do make both values quoted or unquoted? Thanks.
You should always use . equals() when comparing Strings in Java. JUnit calls the . equals() method to determine equality in the method assertEquals(Object o1, Object o2) .
To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include: Carriage return and newline: "\r" and "\n" Backslash: "\\"
How do make both values quoted or unquoted?
The idea of a assertEquals
test is to compare an exact expected value with the output of a method. Here, the DecUtils.decryptToken
is providing a string that already contains open and close quotes. So you have two choices:
Those quotes are expected as part of the return value, so you should change the test string to be the correct expected value:
String data = "\"veryLongEncryptedString.....\"";
Those quotes are not expected as part of the return value: The test is failing because it should be failing, and you have to fix the decryptToken routine so it returns the correct result.
Which one of those to do depends on the documentation for DecUtils.decryptToken
, but please note that you should do only one!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With