0

JUnit assertEquals Changes String
In short: assertEquals("Hello World", "HelloWorld"); appears in the Failure Trace as expected:<Hello[ ]World> but was:<Hello[]World>
Great. Definitely didn't waste my time trying to figure out why the heck my parser was randomly throwing square brackets around.

How do I disable this?

I am using Eclipse 4.19.0 for Java, JUnit 4.10.

Niko O
  • 406
  • 3
  • 15
  • This is a non-problem, IMO - and had the difference in the strings been some other whitespace or non-visible character other than a space, those brackets might have saved you much time allowing you to hone in on the issue more quickly. – crig Aug 31 '21 at 14:15
  • @crig Except that I can double-click the message in the Failure Trace and a window pops up that highlights the differences in color. I just tested it with a space vs nbsp and it highlights the whitespace as expected. However, having magic characters pop up in the middle of the text is visual clutter. Can you tell at a glance what the difference between these two strings is?: ```expected:<[?]`foo`> but was:<[%[]`foo`>``` If you need to squint at this to figure out which `[` is part of the actual string, it's not a non-problem. – Niko O Aug 31 '21 at 14:57
  • In that pop-up window no square brackets are added and you can choose via the right-click menu whether to ignore white space and whether to show white space characters. – howlger Aug 31 '21 at 15:04
  • @NikoO scanning the code, I don't think there's any way to disable this. You've wasted time now trying to work it out, but you'll know for the future. Sorry I can't give you a solution, sometimes things like this crop up. – Andy Turner Aug 31 '21 at 15:19

1 Answers1

1

You cannot.

These brackets are added by junit.framework.ComparisonCompactor that is being used in org.junit.ComparisonFailure assertion error's getMessage method (this error is being thrown by assertEquals btw)

// ComparisonFailure class
public String getMessage() {
    return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage());
}

in ComparisonCompactor these brackets are hardcoded and it seems that there is no configuration that can be provided, also ComparisonCompactor cannot be injected (the same for ComparisonFailure - you are not able to provide custom implementation for them)

public class ComparisonCompactor {

    private static final String ELLIPSIS= "...";
    private static final String DELTA_END= "]";
    private static final String DELTA_START= "[";

    // ...

    private String compactString(String source) {
        String result= DELTA_START + source.substring(fPrefix, source.length() - fSuffix + 1) + DELTA_END;

As far as I see even in Junit 4.13 it looks exactly the same so even bumping up dependency will not help here (however you could give a try to Junit5 that with usage of Assertions.assertEquals will produce expected: <Hello world> but was: <Helloworld> output - but obviously it won't be just bumping up dependency version)

By the way Eclipse has nothing to do with it, the same output you will get in other IDEs (or in console)


What I would suggest (however it's not an answer for your question) is to use assertion library like AssertJ that can give you more control but also make you assertions more fluent.

An example in AssertJ would look like

assertThat("Hello World").isEqualTo("HelloWorld");

and is producing

Expecting:
 <"Hello World">
to be equal to:
 <"HelloWorld">
but was not.
m.antkowicz
  • 13,268
  • 18
  • 37