Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for IntelliJ diffing #277

Closed
wafisher opened this issue Jan 17, 2017 · 10 comments
Closed

Support for IntelliJ diffing #277

wafisher opened this issue Jan 17, 2017 · 10 comments

Comments

@wafisher
Copy link

When I use JUnit's Assert library, IntelliJ will provide a link in the console to a window where I can compare expected vs. actual results. Apparently this is from code like this. Are there any plans to support this with truth?

Thanks!

screenshot 1

@jbduncan
Copy link

@wafisher I'm not on the Truth team, and I could very well be wrong about this as I'm going just by memory here, but I thought Truth was already supported by IntelliJ in this way.

Can you provide a screenshot proving that it doesn't happen for a typical Truth assertion?

@wafisher
Copy link
Author

Unfortunately, all I get is:


java.lang.AssertionError: Not true that <[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> is equal to <[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> (although their toString() representations are the same)

	at com.bowerylabs.position.query.MTAPositionGeneratorTest.getPositions(MTAPositionGeneratorTest.kt:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.AssertionError: Not true that <[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> is equal to <[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> (although their toString() representations are the same)
	at com.google.common.truth.FailureStrategy.fail(FailureStrategy.java:28)
	at com.google.common.truth.FailureStrategy.fail(FailureStrategy.java:22)
	at com.google.common.truth.Subject.failComparingToStrings(Subject.java:335)
	at com.google.common.truth.Subject.doEqualCheck(Subject.java:127)
	at com.google.common.truth.Subject.isEqualTo(Subject.java:104)
	... 28 more

@jbduncan
Copy link

jbduncan commented Feb 9, 2017

@wafisher Yes, I think you're actually right!

I recently encountered in one of my personal projects that IntelliJ didn't give a <Click to see difference> link when a Truth-based assertion in one of my tests failed.

It's not clear to me if there is anything Truth can do about this without resorting to a hack of some kind...

@dinesh707
Copy link

Other than the original question, I have some doubt about the error message (as shown in stack).

java.lang.AssertionError: Not true that
<[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> is equal to
<[1483732224, 1483733032, 1483733309, 1483733459, 1483733969]> (although their toString() representations are the same)

@jbduncan, what was you testing ?, and how could this end up in a situation like this (where all numbers looks same and still assertion error). In your original screenshot you have different values.

@jbduncan
Copy link

Hi @dinesh707, I think you have the wrong end of the stick; you'll have to ask @wafisher, as he's the OP of the issue, not me. :)

@wafisher
Copy link
Author

Oh just bad copying/pasting I think the last one should be ...970.

@dimo414
Copy link
Contributor

dimo414 commented Jul 11, 2017

The issue is that only certain assertions get this diffing treatment - anything that calls .failComparing() should trigger that same <Click to see difference> message in IntelliJ (at least, I've seen it before), but it's generally limited to string comparisons, not arbitrary failures. As you can see in the stack trace Subject.isEqualTo() ends up just calling .fail(), since there's not necessarily a reasonable diff to generate for arbitrary objects.

Generally an easy workaround would be to add an assertion about the objects' .toString() representations:

 assertThat(foo.toString()).isEqualTo(bar.toString());

Though in this case I'm aware this wouldn't work since their .toString() is equivalent.

Other assertions, like IterableSubject.containsExactly(), will report the different elements of the iterable, though I don't imagine IntelliJ will display that nicely. I'm not sure if there's a good way to support that case.

@cpovirk
Copy link
Member

cpovirk commented Sep 19, 2018

As of Truth 0.41, we are throwing ComparisonFailure for most equality assertions. I believe that should be enough for IntelliJ. Let us know if we're missing cases.

@cpovirk cpovirk closed this as completed Sep 19, 2018
@theapache64
Copy link

theapache64 commented Nov 16, 2021

@dimo414 @cpovirk
I tried with the below

 assertThat(foo.toString()).isEqualTo(bar.toString());

but still, IntelliJ doesn't show the Click to see difference action. I am using Truth 1.1.3. Any idea?

@cpovirk
Copy link
Member

cpovirk commented Nov 16, 2021

@theapache64, do you know if you have JUnit 4 on the classpath? Truth currently needs JUnit 4 to produce the "Click to see difference" output. (We'd like to produce it even without JUnit 4 on the classpath, but we haven't done that work yet.)

Truth pulls in JUnit 4 as a transitive Maven dependency by default, but some people exclude it.

(There's some history in #333, but I should summarize it someday.)

If you don't have JUnit 4 on the classpath, are you writing JUnit 5 tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants