Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Spock's
TestLifecycleAware
integration (#2563)
* Fix Spock's `TestLifecycleAware` integration * Small cleanups Co-authored-by: Kevin Wittek <kevin@wittek.dev>
- Loading branch information
Showing
4 changed files
with
112 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 0 additions & 88 deletions
88
modules/spock/src/test/groovy/org/testcontainers/spock/BrowserVncRecordingIT.groovy
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
modules/spock/src/test/groovy/org/testcontainers/spock/TestLifecycleAwareContainerMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.testcontainers.spock; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.lifecycle.TestDescription; | ||
import org.testcontainers.lifecycle.TestLifecycleAware; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class TestLifecycleAwareContainerMock extends GenericContainer<TestLifecycleAwareContainerMock> implements TestLifecycleAware { | ||
|
||
static final String BEFORE_TEST = "beforeTest"; | ||
static final String AFTER_TEST = "afterTest"; | ||
|
||
final List<String> lifecycleMethodCalls = new ArrayList<>(); | ||
final List<String> lifecycleFilesystemFriendlyNames = new ArrayList<>(); | ||
|
||
Throwable capturedThrowable; | ||
|
||
@Override | ||
public void beforeTest(TestDescription description) { | ||
lifecycleMethodCalls.add(BEFORE_TEST); | ||
lifecycleFilesystemFriendlyNames.add(description.getFilesystemFriendlyName()); | ||
} | ||
|
||
@Override | ||
public void afterTest(TestDescription description, Optional<Throwable> throwable) { | ||
lifecycleMethodCalls.add(AFTER_TEST); | ||
throwable.ifPresent(capturedThrowable -> this.capturedThrowable = capturedThrowable); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// Do nothing | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
modules/spock/src/test/groovy/org/testcontainers/spock/TestLifecycleAwareIT.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.testcontainers.spock | ||
|
||
import org.intellij.lang.annotations.Language | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import spock.util.EmbeddedSpecRunner | ||
|
||
class TestLifecycleAwareIT extends Specification { | ||
|
||
@Unroll("When failing test is #fails, afterTest receives '#filesystemFriendlyNames' and throwable starting with message '#errorMessageStartsWith'") | ||
def "lifecycle awareness"() { | ||
given: | ||
|
||
@Language("groovy") | ||
String myTest = """ | ||
import org.testcontainers.spock.Testcontainers | ||
import org.testcontainers.containers.GenericContainer | ||
import spock.lang.Specification | ||
@Testcontainers | ||
class TestLifecycleAwareIT extends Specification { | ||
GenericContainer container = System.properties["org.testcontainers.container"] as GenericContainer | ||
def "perform test"() { | ||
expect: | ||
!System.properties["org.testcontainers.shouldFail"] | ||
} | ||
} | ||
""" | ||
and: | ||
def container = new TestLifecycleAwareContainerMock() | ||
System.properties["org.testcontainers.container"] = container | ||
System.properties["org.testcontainers.shouldFail"] = fails | ||
|
||
when: "executing the test" | ||
def runner = new EmbeddedSpecRunner(throwFailure: false) | ||
runner.run(myTest) | ||
|
||
then: "mock container received lifecycle calls as expected" | ||
container.lifecycleMethodCalls == ["beforeTest", "afterTest"] | ||
container.lifecycleFilesystemFriendlyNames.join(",") == filesystemFriendlyNames | ||
if (errorMessageStartsWith) { | ||
assert container.capturedThrowable.message.startsWith(errorMessageStartsWith) | ||
} else { | ||
assert container.capturedThrowable == null | ||
} | ||
|
||
where: | ||
fails | filesystemFriendlyNames | errorMessageStartsWith | ||
false | 'TestLifecycleAwareIT-perform+test' | null | ||
true | 'TestLifecycleAwareIT-perform+test' | "Condition not satisfied:" | ||
} | ||
|
||
} |