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

Failing assertion message logged twice in IntelliJ IDEA #257

Open
drianmr opened this issue Jan 24, 2022 · 1 comment
Open

Failing assertion message logged twice in IntelliJ IDEA #257

drianmr opened this issue Jan 24, 2022 · 1 comment

Comments

@drianmr
Copy link

drianmr commented Jan 24, 2022

I don't know if it's considered duplicate from #59, but this is just working with a simple assertion.

build.gradle.kts:

dependencies {
    // ...
    testImplementation("io.strikt:strikt-core:0.33.0")
    // ...
}

Example code:

package com.example

import kotlin.test.Test
import strikt.api.expect
import strikt.api.expectThat
import strikt.assertions.isEqualTo
import strikt.assertions.isNotEqualTo

class Test {

    @Test
    fun testSingleSubject() {
        expectThat(1).isNotEqualTo(1)
    }

    @Test
    fun testMultipleSubject() {
        expect {
            that(1).isEqualTo(1)
            that(1).isEqualTo(2)
            that(1).isNotEqualTo(3)
            that(1).isNotEqualTo(4)
        }
    }
}

Output:

▼ Expect that 1:
  ✓ is equal to 1
▼ Expect that 1:
  ✗ is equal to 2
          found 1
▼ Expect that 1:
  ✓ is not equal to 3
▼ Expect that 1:
  ✓ is not equal to 4
▼ Expect that 1:
  ✓ is equal to 1
▼ Expect that 1:
  ✗ is equal to 2
          found 1
▼ Expect that 1:
  ✓ is not equal to 3
▼ Expect that 1:
  ✓ is not equal to 4
	at strikt.internal.AssertionStrategy$Throwing.evaluate(AssertionStrategy.kt:160)
	at strikt.api.ExpectKt.expect(Expect.kt:27)
	at com.example.Test.testMultipleSubject(Test.kt:18)
	...


▼ Expect that 1:
  ✗ is not equal to 1
▼ Expect that 1:
  ✗ is not equal to 1
	at com.example.Test.testSingleSubject(Test.kt:13)
	...
	Suppressed: org.opentest4j.AssertionFailedError
		at strikt.internal.AssertionStrategy.createAssertionFailedError$strikt_core(AssertionStrategy.kt:220)
		...


But if I run the test in terminal it's working fine.
(I added testLogging block to show the log in terminal since it does not affect the IntelliJ IDEA output)

build.gradle.kts:

// ...

tasks.withType<Test> {
    testLogging {
        events(STARTED, PASSED, SKIPPED, FAILED)
        exceptionFormat = FULL
        showCauses = true
        showExceptions = true
        showStackTraces = true
    }
}

Command:

./gradlew test --tests "com.example.Test"

Output:

> Task :test FAILED

com.example.Test > testMultipleSubject STARTED

com.example.Test > testMultipleSubject FAILED
    ▼ Expect that 1:
      ✓ is equal to 1
    ▼ Expect that 1:
      ✗ is equal to 2
              found 1
    ▼ Expect that 1:
      ✓ is not equal to 3
    ▼ Expect that 1:
      ✓ is not equal to 4
        at strikt.internal.AssertionStrategy$Throwing.evaluate(AssertionStrategy.kt:160)
        at strikt.api.ExpectKt.expect(Expect.kt:27)
        at com.example.Test.testMultipleSubject(Test.kt:18)

com.example.Test > testSingleSubject STARTED

com.example.Test > testSingleSubject FAILED
    ▼ Expect that 1:
      ✗ is not equal to 1
        at com.example.Test.testSingleSubject(Test.kt:13)

2 tests completed, 2 failed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.

IntelliJ IDEA Information

IntelliJ IDEA 2021.3.1 (Community Edition)
Build #IC-213.6461.79, built on December 28, 2021
Runtime version: 11.0.13+7-b1751.21 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-96-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 864M
Cores: 8
Non-Bundled Plugins:
    org.jetbrains.kotlin (213-1.6.10-release-923-IJ5744.223)
    io.ktor.intellij.plugin (1.5.1-obsolete)
Kotlin: 213-1.6.10-release-923-IJ5744.223
Current Desktop: ubuntu:GNOME
@yuriykulikov
Copy link

It is a bit confusing, but actually it is not logged twice.

First the strikt.internal.opentest4j.AssertionFailed.message is logged and then strikt.internal.opentest4j.AssertionFailed stacktrace is printed. Usually, the first line in the stacktrace looks something like this: org.opentest4j.AssertionFailedError: expected: <expected> but was: <actual> (exception class name and the message).

Then the whole thing looks like this:

expected: <expected> but was: <actual>
org.opentest4j.AssertionFailedError: expected: <expected> but was: <actual>
	at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)

But strikt.internal.opentest4j.AssertionFailed prints only the message without the class name, so it becomes:

expected: <expected> but was: <actual>
expected: <expected> but was: <actual>
	at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)

Since this message is multiline, it looks as if it was printed twice.

In my opinion, printing the class name in toString() would be consistent with other exception classes.

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

2 participants