In JUnit 3, I could get the name of the currently running test like this:
public class MyTest extends TestCase {
public void testSomething() {
assertThat(getName(), is("testSomething"));
}
}
How do I do this in spock? I would like to use the test name as a key in a shared resource so that tests don't interfere with each other.
Stubs are fake classes that come with preprogrammed return values. Mocks are fake classes that we can examine after a test has finished and see which methods were run or not. Spock makes a clear distinction between the two as mocks and stubs , as we will see in the sections to follow.
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language. Thanks to its JUnit runner, Spock is compatible with most IDEs, build tools, and continuous integration servers.
lang. Specification class. A Spock specification can have instance fields, fixture methods, feature methods, and helper methods. We should prefer normal instance fields because they help us to isolate feature methods from each other.
One solution is to leverage JUnit's TestName rule:
import org.junit.Rule
import org.junit.rules.TestName
class MySpec extends Specification {
@Rule TestName name = new TestName()
def "some test"() {
expect: name.methodName == "some test"
}
}
This requires JUnit 4.7 or higher.
For spock 1.0-groovy-2.4 you can try :
def "Simple test"() {
expect:
specificationContext.currentIteration.name == "Simple test"
}
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