Skip to content

Commit 66babb0

Browse files
author
Ronald Holshausen
authoredFeb 18, 2020
Merge pull request #1021 from treatwell/defer-junit-runner-initialization
Defer Runner initialization until it is run
2 parents da08b2f + e533511 commit 66babb0

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed
 

‎provider/pact-jvm-provider-junit/src/main/kotlin/au/com/dius/pact/provider/junit/PactRunner.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ open class PactRunner<I>(clazz: Class<*>) : ParentRunner<InteractionRunner<I>>(c
5353

5454
private val child = mutableListOf<InteractionRunner<I>>()
5555
private var valueResolver = SystemPropertyResolver()
56+
private val clazz = clazz
57+
private var initialized = false
58+
59+
override fun run(notifier: RunNotifier?) {
60+
initialize()
61+
62+
super.run(notifier)
63+
}
64+
65+
private fun initialize() {
66+
if (initialized) {
67+
return
68+
}
5669

57-
init {
5870
if (clazz.getAnnotation(Ignore::class.java) != null) {
5971
logger.info("Ignore annotation detected, exiting")
6072
} else {
@@ -110,6 +122,7 @@ open class PactRunner<I>(clazz: Class<*>) : ParentRunner<InteractionRunner<I>>(c
110122

111123
setupInteractionRunners(testClass, pacts, pactLoader)
112124
}
125+
initialized = true
113126
}
114127

115128
protected open fun setupInteractionRunners(testClass: TestClass, pacts: List<Pact<I>>, pactLoader: PactLoader) {

‎provider/pact-jvm-provider-junit/src/test/groovy/au/com/dius/pact/provider/junit/FilteredPactRunnerSpec.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import au.com.dius.pact.provider.junit.loader.PactFilter
1010
import au.com.dius.pact.provider.junit.loader.PactFolder
1111
import au.com.dius.pact.provider.junit.target.Target
1212
import au.com.dius.pact.provider.junit.target.TestTarget
13+
import org.junit.runner.notification.RunNotifier
1314
import org.junit.runners.model.InitializationError
1415
import spock.lang.Specification
1516

@@ -189,7 +190,7 @@ class FilteredPactRunnerSpec extends Specification {
189190
@SuppressWarnings('UnusedObject')
190191
def 'Throws an initialisation error if all pacts are filtered out'() {
191192
when:
192-
new PactRunner(TestFilterOutAllPactsClass)
193+
new PactRunner(TestFilterOutAllPactsClass).run(new RunNotifier())
193194

194195
then:
195196
thrown(InitializationError)

‎provider/pact-jvm-provider-junit/src/test/groovy/au/com/dius/pact/provider/junit/PactRunnerSpec.groovy

+22-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import au.com.dius.pact.provider.junit.loader.PactUrl
1010
import au.com.dius.pact.provider.junit.loader.PactUrlLoader
1111
import au.com.dius.pact.provider.junit.target.Target
1212
import au.com.dius.pact.provider.junit.target.TestTarget
13+
import org.junit.runner.notification.RunNotifier
1314
import org.junit.runners.model.InitializationError
1415
import spock.lang.Specification
1516

@@ -115,7 +116,7 @@ class PactRunnerSpec extends Specification {
115116

116117
def 'PactRunner throws an exception if there is no @Provider annotation on the test class'() {
117118
when:
118-
new PactRunner(PactRunnerSpec)
119+
new PactRunner(PactRunnerSpec).run(new RunNotifier())
119120

120121
then:
121122
InitializationError e = thrown()
@@ -125,7 +126,7 @@ class PactRunnerSpec extends Specification {
125126

126127
def 'PactRunner throws an exception if there is no pact source'() {
127128
when:
128-
new PactRunner(NoSourceTestClass)
129+
new PactRunner(NoSourceTestClass).run(new RunNotifier())
129130

130131
then:
131132
InitializationError e = thrown()
@@ -134,7 +135,7 @@ class PactRunnerSpec extends Specification {
134135

135136
def 'PactRunner throws an exception if the pact source throws an IO exception'() {
136137
when:
137-
new PactRunner(FailsTestClass)
138+
new PactRunner(FailsTestClass).run(new RunNotifier())
138139

139140
then:
140141
InitializationError e = thrown()
@@ -143,13 +144,26 @@ class PactRunnerSpec extends Specification {
143144

144145
def 'PactRunner throws an exception if there are no pacts to verify'() {
145146
when:
146-
new PactRunner(NoPactsTestClass)
147+
new PactRunner(NoPactsTestClass).run(new RunNotifier())
147148

148149
then:
149150
InitializationError e = thrown()
150151
e.causes*.message == ['Did not find any pact files for provider Bob']
151152
}
152153

154+
def 'PactRunner only initializes once if run() is called multiple times'() {
155+
when:
156+
def runner = new PactRunner(TestClass)
157+
runner.run(new RunNotifier())
158+
def children1 = runner.children.clone()
159+
160+
runner.run(new RunNotifier())
161+
def children2 = runner.children.clone()
162+
163+
then:
164+
children1 == children2
165+
}
166+
153167
def 'PactRunner does not throw an exception if there are no pacts to verify and @IgnoreNoPactsToVerify'() {
154168
when:
155169
new PactRunner(NoPactsIgnoredTestClass)
@@ -169,7 +183,7 @@ class PactRunnerSpec extends Specification {
169183

170184
def 'PactRunner throws an exception if there is both a pact source and pact loader annotation'() {
171185
when:
172-
new PactRunner(BothPactSourceAndPactLoaderTestClass)
186+
new PactRunner(BothPactSourceAndPactLoaderTestClass).run(new RunNotifier())
173187

174188
then:
175189
InitializationError e = thrown()
@@ -179,6 +193,7 @@ class PactRunnerSpec extends Specification {
179193
def 'PactRunner handles a pact source with a pact loader that takes a class parameter'() {
180194
when:
181195
def runner = new PactRunner(PactLoaderWithConstructorParameterTestClass)
196+
runner.run(new RunNotifier())
182197

183198
then:
184199
!runner.children.empty
@@ -187,6 +202,7 @@ class PactRunnerSpec extends Specification {
187202
def 'PactRunner handles a pact source with a pact loader that does not takes a class parameter'() {
188203
when:
189204
def runner = new PactRunner(PactLoaderWithDefaultConstructorClass)
205+
runner.run(new RunNotifier())
190206

191207
then:
192208
!runner.children.empty
@@ -195,6 +211,7 @@ class PactRunnerSpec extends Specification {
195211
def 'PactRunner loads the pact loader class from the pact loader associated with the pact loader annotation'() {
196212
when:
197213
def runner = new PactRunner(TestClass)
214+
runner.run(new RunNotifier())
198215

199216
then:
200217
!runner.children.empty

0 commit comments

Comments
 (0)
Please sign in to comment.