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

Builder<T>.single() throws wrong exception when used inside and{ ... } #203

Closed
MartinHaeusler opened this issue Jan 31, 2020 · 2 comments
Closed
Labels

Comments

@MartinHaeusler
Copy link

@MartinHaeusler MartinHaeusler commented Jan 31, 2020

Hi,

I use strikt quite extensively by now, and I noticed that the single() assertion sometimes does not produce an assertion error, but rather an IllegalArgumentException. The test will fail either way (that's fine), but from the resulting error message it's hard to tell WHAT exactly failed.

Here's a minimal self-contained example which will produce an IllegalArgumentException, but should actually produce an assertion error (with the usual nice strikt messages):

class StriktTest {

    @Test
    fun striktTest(){
        class Person(val firstName: String, val hobbies: List<String>)

        // imagine that "p" is coming from some API call and needs to be nullable
        val p: Person? = Person("John", listOf("a","b"))

        expectThat(p).isNotNull().and {
            get { firstName }.isEqualTo("John")
            get { hobbies }.single().isA<String>().isEqualTo("a")
        }
    }
}

Here's the relevant portion of the stack trace I'm getting:

java.lang.IllegalArgumentException: List has more than one element.

	at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:519)
	at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:499)
	at strikt.assertions.IterableKt$single$2.invoke(Iterable.kt:29)
	at strikt.assertions.IterableKt$single$2.invoke(Iterable.kt)
	at strikt.internal.AssertionBuilder.get(AssertionBuilder.kt:92)
	at strikt.assertions.IterableKt.single(Iterable.kt:29)
	at org.example.test.StriktTest$striktTest$1.invoke(StriktTest.kt:21)
	at org.example.test.StriktTest$striktTest$1.invoke(StriktTest.kt:10)
	at strikt.internal.AssertionBuilder.and(AssertionBuilder.kt:37)

As you can see, it's hard to tell what went wrong, especially if there are multiple .single() assertions in a compound expectThat(...). I tried to look deeper into the issue, but I'm too unfamiliar with the internal context handling to make real sense of it right now. All I know is: it only seems to happen if single() is used inside an and { ... } clause.

@MartinHaeusler MartinHaeusler changed the title Builder<T>.single() throws wrong exception under specific circumstances Builder<T>.single() throws wrong exception when used inside and{ ... } Jan 31, 2020
@robfletcher robfletcher added the 🐛 bug label Feb 8, 2020
@robfletcher

This comment has been minimized.

Copy link
Owner

@robfletcher robfletcher commented Feb 8, 2020

I'll get this fixed. Seems similar to a bug I dealt with a little while ago.

@MartinHaeusler

This comment has been minimized.

Copy link
Author

@MartinHaeusler MartinHaeusler commented Feb 8, 2020

That would be great. The test fails either way, but it's hard to tell what's going on with the message it is currently printing.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.