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

Spock tests are executing @BeforeEach but not @AfterEach #157

Closed
longwa opened this issue Nov 14, 2021 · 1 comment
Closed

Spock tests are executing @BeforeEach but not @AfterEach #157

longwa opened this issue Nov 14, 2021 · 1 comment
Assignees

Comments

@longwa
Copy link

longwa commented Nov 14, 2021

Some of the extensions provided in this library are causing specs to execute methods annotated with JUnit5's @BeforeEach but not @AfterEach.

I'm not sure if this library is trying to emulate the @BeforeEach or if it's just something leftover, but the current behavior is not super useful.

Critically, the ordering of the calls is different than in Grails 4.0, i.e it used to be:

beforeEach
setup
test method
cleanup
afterEach

Now the ordering is:

setup
beforeEach
test method
cleanup

This makes it difficult to use @BeforeEach in a trait or base class as usually you will want that to be called first (which it used to be). Also, Spock makes it difficult since traits can't use setup() if the test class also has a setup method.

IMO, this library should implement an extension that creates compatibility with Before* and After* annotations while keeping the test order consistent with pre-5.0 behavior (and what would be considered the sensical ordering)

package test

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import spock.lang.Specification

class TestAnnotationSpec extends Specification {

    def "test method 1"() {
        when:
        println "test method 1"

        then:
        true
    }

    @BeforeEach
    void beforeEach() {
        println "beforeEach"
    }

    @AfterEach
    void afterEach() {
        println "afterEach"
    }

    void setup() {
        println "setup"
    }

    void cleanup() {
        println "cleanup"
    }
}
@longwa
Copy link
Author

longwa commented Nov 14, 2021

This was actually very easy to fix just by changing the TestingSupportExtension.

Essentially we just need to prepend the BeforeEach to the setupMethods and then add a block to handle the AfterEach. I will also add handlers for the BeforeAll and AfterAll. That should provide a nice compatibility layer between Spock and JUnit5 when using Grails 5.

I will submit a PR shortly.

longwa added a commit to longwa/grails-testing-support that referenced this issue Nov 14, 2021
longwa added a commit to longwa/grails-testing-support that referenced this issue Nov 14, 2021
jdaugherty pushed a commit to jdaugherty/grails-testing-support that referenced this issue Sep 25, 2024
jdaugherty pushed a commit to jdaugherty/grails-testing-support that referenced this issue Sep 25, 2024
@jdaugherty jdaugherty self-assigned this Sep 25, 2024
jdaugherty pushed a commit to jdaugherty/grails-testing-support that referenced this issue Sep 26, 2024
jdaugherty pushed a commit to jdaugherty/grails-testing-support that referenced this issue Sep 26, 2024
jdaugherty pushed a commit to jdaugherty/grails-testing-support that referenced this issue Sep 26, 2024
jdaugherty added a commit to jdaugherty/grails-testing-support that referenced this issue Sep 26, 2024
jdaugherty added a commit to jdaugherty/grails-testing-support that referenced this issue Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants