Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spock data driven testing with classes to test for "instanceof" assertions

I have a test where I want to test a specific result to be of a kind of a class. Asserting this with the instanceof keyword. Sadly I did not figured out how to actually provide a data value expectedClass in the data table without having a Problem to actually call it with instanceof. I think the keyword instanceof is the issue here because it only accepts class/interface names. Any idea how I can test what I want to test in another way?

class BattleResolverFactoryTest extends Specification {

    def uut = new BattleResolverFactory()

    @Unroll
    def "should return proper BattleResolver for given ConflictType"(Conflict.ConflictType conflictType, Class<? extends BattleResolver> expectedInstance) {
        when:
        def battleResolver = BattleResolverFactory.getResolver(conflictType)
        then:
        battleResolver instanceof expectedClass
        where:
        conflictType                            | expectedClass
        Conflict.ConflictType.INFANTRY_CONFLICT | TestInfantryResolver
        Conflict.ConflictType.ARMOR_CONFLICT    | TestArmorResolver

    }
}
like image 917
xetra11 Avatar asked Jun 24 '17 19:06

xetra11


People also ask

Is Spock better than JUnit?

The main difference between Spock and JUnit is in breadth of capabilities. Both frameworks can be used for unit testing. However, Spock offers much more — including mocking and integration testing. JUnit requires a third party library for mocking.

How do I run Spock test in IntelliJ?

Inside the class, press Alt+Insert and select Test Method to generate test methods for the Spock specification. This can be especially useful when you are not so familiar with writing Spock or Groovy code, as IntelliJ IDEA will generate the basic structure that you need.

What is Groovy test?

Groovy Programming Fundamentals for Java Developers The fundamental unit of an object-oriented system is the class. Therefore unit testing consists of testig within a class. The approach taken is to create an object of the class under testing and use it to check that selected methods execute as expected.

What is @shared in groovy?

@Shared is a Spock feature that says to the reader that this variable is the same for all feature methods. It is an instruction specifically for the unit test and makes the unit test more clear for the reader. The same can be said for the main Spock blocks.


2 Answers

You can use isCase like so:

class BattleResolverFactoryTest extends Specification {

    def uut = new BattleResolverFactory()

    @Unroll
    def "should return proper BattleResolver for given ConflictType"(Conflict.ConflictType conflictType, Class<? extends BattleResolver> expectedInstance) {
        when:
        def battleResolver = BattleResolverFactory.getResolver(conflictType)

        then:
        expectedClass.isCase(battleResolver)

        where:
        conflictType                            | expectedClass
        Conflict.ConflictType.INFANTRY_CONFLICT | TestInfantryResolver
        Conflict.ConflictType.ARMOR_CONFLICT    | TestArmorResolver

    }
}
like image 86
tim_yates Avatar answered Oct 01 '22 20:10

tim_yates


As tim_yates said you can use the groovy function isCase or the java function isAssignableFrom, or you could use the groovy in keyword (which delegates to isCase).

  • expectedClass.isCase(battleResolver)
  • expectedClass.isAssignableFrom(battleResolver)
  • expectedClass in battleResolver
like image 28
Leonard Brünings Avatar answered Oct 01 '22 19:10

Leonard Brünings