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
}
}
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.
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.
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.
@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.
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
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With