You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JUnit 4字符串比较引号不一致:如何统一两者引号状态?

Fixing JUnit 4 assertEquals Quote Mismatch in Java 8

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 strip method 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:06:50