Skip to content

Commit

Permalink
fix(JUnit5): do not overwrite class level provider name is the method…
Browse files Browse the repository at this point in the history
… one is empty
  • Loading branch information
rholshausen committed Apr 11, 2023
1 parent f726b0d commit 9aebbb5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import au.com.dius.pact.core.support.MetricEvent
import au.com.dius.pact.core.support.Metrics
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser
import au.com.dius.pact.core.support.isNotEmpty
import mu.KLogging
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -364,7 +365,9 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
methodAnnotation != null -> if (methodAnnotation.pactMethods.isNotEmpty()) {
methodAnnotation.pactMethods.map {
val providerName = providerNameFromPactMethod(it, context)
val provider = providerInfo.copy(providerName = providerName)
val provider = if (providerName.isNotEmpty())
providerInfo.copy(providerName = providerName)
else providerInfo
val mockServerConfig = mockServerConfigFromAnnotation(context, provider)
provider.withMockServerConfig(mockServerConfig) to it
}
Expand All @@ -376,7 +379,9 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
classAnnotation != null -> if (classAnnotation.pactMethods.isNotEmpty()) {
classAnnotation.pactMethods.map {
val providerName = providerNameFromPactMethod(it, context)
val provider = providerInfo.copy(providerName = providerName)
val provider = if (providerName.isNotEmpty())
providerInfo.copy(providerName = providerName)
else providerInfo
val mockServerConfig = mockServerConfigFromAnnotation(context, provider)
provider.withMockServerConfig(mockServerConfig) to it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.com.dius.pact.consumer.junit5

import au.com.dius.pact.consumer.BaseMockServer
import au.com.dius.pact.consumer.PactVerificationResult
import au.com.dius.pact.consumer.dsl.PactDslWithProvider
import au.com.dius.pact.consumer.junit.MockServerConfig
import au.com.dius.pact.consumer.model.MockProviderConfig
import au.com.dius.pact.core.model.Consumer
Expand All @@ -10,6 +11,7 @@ import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.RequestResponseInteraction
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.annotations.Pact
import au.com.dius.pact.core.model.messaging.MessagePact
import au.com.dius.pact.core.support.BuiltToolConfig
import groovy.json.JsonSlurper
Expand Down Expand Up @@ -226,6 +228,30 @@ class PactConsumerTestExtSpec extends Specification {
providerInfo.first().second == ''
}

@PactTestFor(providerName = 'TestClassEmptyProviderOnMethod', pactVersion = PactSpecVersion.V3)
static class TestClassEmptyProviderOnMethod {
@Pact
RequestResponsePact pactMethod(PactDslWithProvider builder) { builder.toPact() }

@PactTestFor(pactMethods = [ 'pactMethod' ])
def pactTestForMethod() { }
}

def 'lookupProviderInfo - do not overwrite the class level values if the method level one is empty'() {
given:
testMethod = TestClassEmptyProviderOnMethod.getMethod('pactTestForMethod')
requiredTestClass = TestClassEmptyProviderOnMethod

when:
def providerInfo = testExt.lookupProviderInfo(mockContext)

then:
providerInfo.size() == 1
providerInfo.first().first.providerName == 'TestClassEmptyProviderOnMethod'
providerInfo.first().first.pactVersion == PactSpecVersion.V3
providerInfo.first().second == 'pactMethod'
}

def 'mockServerConfigured - returns false when there are no MockServerConfig annotations'() {
expect:
!testExt.mockServerConfigured(mockContext)
Expand Down

0 comments on commit 9aebbb5

Please sign in to comment.