0

The latest AssertJ release notes have brought to my attention that I haven't really experimented much with assertj assumptions, so I thought I'd play around with it a bit:

object AssertjAssumptions {

    @MethodSource("starWarsCharacters")
    @ParameterizedTest
    fun `light-aligned force users`(character: ForceUser){
        given(character.alignment).isEqualTo(LIGHT)

        then(character.lightsaberColour).isNotEqualTo(RED)
    }

    @MethodSource("starWarsCharacters")
    @ParameterizedTest
    fun `dark-aligned force users`(character: ForceUser){
        given(character.alignment).isEqualTo(DARK)

        then(character.lightsaberColour).isIn(RED, UNKNOWN)
    }

    enum class Alignment {
        LIGHT, DARK, WEIRD
    }

    enum class LightsaberColour{
        BLUE, GREEN, YELLOW, PURPLE, RED, WHITE, UNKNOWN
    }

    data class ForceUser(val name: String, val alignment: Alignment, val lightsaberColour: LightsaberColour)

    @JvmStatic fun starWarsCharacters() = listOf(
        ForceUser("2nd Sister", DARK, RED),
        ForceUser("Ahsoka Tano", WEIRD, WHITE),
        ForceUser("Asajj Ventress", DARK, RED),
        ForceUser("Cal Kestis", LIGHT, UNKNOWN),
        ForceUser("Count Dooku", DARK, RED),
        ForceUser("Darth Maul", DARK, RED),
        ForceUser("Darth Vader", DARK, RED),
        ForceUser("Ki-Adi-Mundi", LIGHT, BLUE),
        ForceUser("Kylo Ren", WEIRD, RED),
        ForceUser("Luke Skywalker", LIGHT, GREEN),
        ForceUser("Mace Windu", WEIRD, PURPLE),
        ForceUser("Obi-Wan Kenobi", LIGHT, BLUE),
        ForceUser("Qui-Gon Jinn", LIGHT, GREEN),
        ForceUser("Quinlan Vos", WEIRD, YELLOW),
        ForceUser("Rey", WEIRD, BLUE),
        ForceUser("Sheev Palpatine", DARK, RED),
        ForceUser("Snoke", DARK, UNKNOWN),
        ForceUser("Yoda", LIGHT, GREEN)
    )
}

This seems to work, but reports

Tests passed: 13, ignored: 23 of 36 tests

Which is suboptimal.

Can I somehow tell JUnit that for these tests, "ignored" means the test can be treated as non-existent?

User1291
  • 7,664
  • 8
  • 51
  • 108
  • what exactly do you think ignored does? It doesn't execute those tests, so basically, they are treated as non-existent – Stultuske Dec 17 '19 at 13:17
  • @Stultuske yes, but they're still reported as "ignored". I want them gone completely. – User1291 Dec 17 '19 at 13:19
  • 1
    Assumptions are what they are... assumptions... you assumed some things to be true, but they weren't... as soon as an assumption doesn't hold, the test is ignored... I wouldn't use assumptions for such a test as you have shown.... instead I would rather add an additional intermediate function such as: `fun lightAlignedStarWarsCharacters() = starWarsCharacters().filter { it.alignment == LIGHT }` and use that function instead as input for the light-aligned test... – Roland Dec 17 '19 at 13:21
  • @User1291 so you don't want to execute them? you don't want them mentioned in a report? delete them, or completely comment them out. Normally, ignoring tests is not because you don't need them anymore, you just temporarily don't want to run them. In that case, it is good to be reminded that there are ignored tests. If you don't want that, and you don't want to run the tests, kind of seems you just want them gone totally. As Captain Picard would say: Make it so! – Stultuske Dec 17 '19 at 13:26
  • 1
    @Stultuske I think you may want to take another look at the code example I posted. Because I don't want the ENTIRE test gone, just the test cases that don't match the assumptions. – User1291 Dec 17 '19 at 13:32
  • Assumptions are helpful, if you need to ensure that certain conditions are met, before the actual test is executed... but every assumption that does not hold needs to be marked somehow (will be ignored)... You know that the light-aligned-test will only just ever check light-aligned characters... so it isn't an assumption that you have light-aligned characters,... it's a fact... same goes for the dark side... I think the given/when/then-style makes it less obvious why assumptions are so important... maybe it helps if you think of it as `assumeThat(character).isLightAligned()`? – Roland Dec 17 '19 at 13:36
  • The javadoc of `Assume` (which I'd guess this is translated to) says: "The default JUnit runner skips tests with failing assumptions. Custom runners may behave differently." so writing an own runner might be an option. I haven't done it, though. – Marvin Dec 17 '19 at 13:46
  • I'm not sure this is a proper use of assumptions. You appear to be using JUnit 5 whose [documentation](https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html) implies assumptions are meant more for assuming something about the testing _environment_. The [examples](https://junit.org/junit5/docs/current/user-guide/#writing-tests-assumptions) given in the _User Guide_ all deal with whether or not the tests are being performed on the local dev machine or a CI server. – Slaw Dec 17 '19 at 14:27

0 Answers0