-
Notifications
You must be signed in to change notification settings - Fork 64
Assert with JUnit for easier debugging in Android Studio #177
Comments
Hi 👋 and thanks for raising this issue! I remember that we were using junit 4 a while back, but I can’t remember or find the reason we switched to The reason for us using junit was the same you’re talking about, the great experience in IDEs, including diff viewers for comparison failures. I think it might be a good idea to raise an issue at JetBrains and see what they think about implementing the same behavior for Depending on their answer, I think we could go and either implement formatters ourselves or fall back to junit again for JVM and Android 🙂 |
Ok, I've been looking up things in Kotlin's source code for hours and this isn't a bug on The folks at JetBrains seem to want to provide good testing tools without getting coupled to a single testing framework. Naturally, Kluent should do the same. Under the hood So, to get diff viewers with Kluent, all I have to do is add this line to my testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" No additional configuration is required. Maybe we could add this to the README. |
Anither thing worth looking at is if we can reuse But in the meantime I'll add your solution to the README 🎉 |
I think "expected:<$expectedObj> but was:<$actualObj>" |
I've added a note to the README :-) |
I've been using Kluent in Android projects for years because the syntax is great and I really like writing tests with it. However, there has always been a little problem when comparing "big" structures like data classes or with several fields or long lists. The error message printed by assertion methods of
kotlin.test
get really hard to read when thetoString()
methods of the compared objects are very large. For reference here is how it is implemented:As you can see the message gets printed on a single line. When it gets wrapped in a terminal it's really hard to figure out what is going on. Consider this example that compares lists of timestamps:
One of them is off by a single digit. This is the console output :
This list isn't really that long, but it's really hard to spot that wrong digit. It would be nice if the objects could be printed on different lines so that they can be compared side by side. Unfortunately no matter how we print it, it's going to be hard figure out errors on a console because
toString()
implementations don't follow any standards whatsoever.I think the best bet here is to leave this job to existing tooling. Android Studio has great integration with JUnit 4. When you create a new project, you get example tests generated with JUnit 4. This tests use
org.junit.Assert.*
methods for assertion. WhenassertEquals
fails it prints each object on a different line as I suggested earlier. But the really cool thing here is that when the assertion fails you get a nice "Click to see difference" link that opens a new window that highlights the difference. You can see it here:To get this in my tests I can easily implement
should be equal to
like this:This would be pretty easy to implement, but it's a significant change nevertheless. A good thing is that the API remains the same. The only problem that I see is that if your setup doesn't include JUnit 4, then there aren't much benefits. I'd assume that if this works on Android Studio, it works on all of JetBrains IDEs as well, but I really don't know. Let me know what you guys think.
The text was updated successfully, but these errors were encountered: