Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit fails- quotes in String comparing

Tags:

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.

like image 200
Itsik Mauyhas Avatar asked May 30 '18 08:05

Itsik Mauyhas


People also ask

How do I compare two strings in JUnit?

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) .

How to remove 2 double quotes from string in Java?

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.

How do you escape quotes in Java?

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: "\\"


1 Answers

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!

like image 162
Bob Jacobsen Avatar answered Oct 13 '22 01:10

Bob Jacobsen