JUnit 4字符串比较引号不一致:如何统一两者引号状态?
It looks like your decryptToken method is returning the decrypted value wrapped in double quotes, but your expected string doesn’t include them—that’s exactly why JUnit is showing the mismatch between <[xxx]> and <["xxx"]>. Here are a few straightforward ways to get them aligned:
Option 1: Update the Expected Value to Match the Actual Output
If the quoted output is intentional (like the token is supposed to include quotes as part of its structure), just add escaped quotes to your expected string in the test:
assertEquals("Here User name: ", "\"encrypt expected value\"", value);
Java uses \" to escape double quotes inside a string literal, so this will make your expected value perfectly match the quoted actual result.
Option 2: Strip Quotes from the Actual Value Before Comparing
If the quotes are unintended (a quirk or bug in decryptToken), you can clean up the actual value before running the assertion. Try one of these approaches:
- Remove all double quotes from the result:
String cleanedValue = value.replace("\"", ""); assertEquals("Here User name: ", "encrypt expected value", cleanedValue); - Or, if you’re certain the value is wrapped in exactly one pair of quotes at the start and end:
String cleanedValue = value.substring(1, value.length() - 1); assertEquals("Here User name: ", "encrypt expected value", cleanedValue); - If you use Apache Commons Lang, leverage the handy
stripmethod to remove surrounding quotes:String cleanedValue = StringUtils.strip(value, "\""); assertEquals("Here User name: ", "encrypt expected value", cleanedValue);
Option 3: Fix the decryptToken Method (Best Long-Term Fix)
If the quotes shouldn’t be there in the first place, dive into the DecUtils.decryptToken implementation. The null parameter you’re passing might be triggering a code path that wraps the result in quotes. Adjust the method to return the raw decrypted value without extra formatting, and your test will pass without needing workarounds.
That should resolve the mismatch and get your test passing smoothly!
内容的提问来源于stack exchange,提问作者Itsik Mauyhas

