botismarius
botismarius

Reputation: 3045

Formatting output so that Intellij Idea shows diffs for two texts

I would like to be able to print in the logs a message for which intellij idea would present a nice way of comparing two objects (strings). This happens automatically for the error message logged by a failed junit assert:

assertEquals("some\nString", "another\nString");

=>
org.junit.ComparisonFailure:  <Click to see difference>
    at org.junit.Assert.assertEquals(Assert.java:123)
    at org.junit.Assert.assertEquals(Assert.java:145)
    at com.something.DummyTest.testDummy(DummyTest.java:89)

The <Click to see difference> entry is actually displayed as a link in the output window of the Intellij Idea. When you click on the link, a compare window opens which shows the two values (just like you would compare two files).

Simply throwing an exception is not acceptable because I would like to log multiple objects to compare. I already tried logging a text, but I wasn't able to convince idea to compare the two texts.

Upvotes: 29

Views: 11128

Answers (2)

Sim0rn
Sim0rn

Reputation: 802

I had the same Problem and found the solution in https://github.com/joel-costigliola/assertj-core/issues/1364#issuecomment-440800958

You should throw an org.junit.ComparisonFailure. Then IntelliJ will display the <Click to see difference>

Upvotes: 5

CrazyCoder
CrazyCoder

Reputation: 402493

IntelliJ IDEA is using the hardcoded regular expression. If the text matches the pattern, it will suggest to click to view the difference.

The pattern is:

expected:<bla-blah> but was:<blah-blah-blah>

Output should match the format of assertEquals or assertThat.

The exact patterns are somewhat scattered around the code in IDEA, but some are e.g. here.

Upvotes: 42

Related Questions