Home > Mobile >  Best approach to test several similar classes in spock test?
Best approach to test several similar classes in spock test?

Time:03-29

I have a lot of similar classes (actually it is different types of events with one parent class). It is about 30 classes already and the number will be growing. Every class has its own logic to process, but there several fields that are exists in every class. I want to be sure every event's flow is taking care of common fields. It is become more complex, because of adding new event types and adding new flows. The best approach will be to create some dynamic test that will be checking common fields are processed. Saying 'dynamically' I mean the ability of the test automatically discover new classes and put them into the test pack. We are using spock, but it is not possible to dynamically generate 'where' section of test. I come with quite a strange approach that is not working, but illustrate my idea:

def "dynamic test"() {
    given:
        def classes = methodToGetListOfEventClass()

    when:
        for(Class clazz : classes) {
                    ParentEvent event = clazz.getDeclaredConstructor().newInstance() as ParentEvent
                    service.sendEvent(event)
                }
            }

    then:
        for(Class clazz : classes) {
                    ParentEvent event = clazz.getDeclaredConstructor().newInstance() as ParentEvent
                    1 * sendExternalEvent("someId", event.getClass().getName(), Collections.emptyMap())
                    //check common fields exists
                    }
                }
            }

}

So I just try to create an instance of every class, pass it into the event handler and check created external event has all common fields set. It looks ugly and does not work. Is any suggestion on how to implement such a dynamic test?

CodePudding user response:

You can use dynamic IDE screenshot

  • Related