Skip to content

Commit

Permalink
Support using other JUnit @disabled.. annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Dec 9, 2024
1 parent 80d8a87 commit e88957a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ import au.com.dius.pact.core.support.isNotEmpty
import io.github.oshai.kotlinlogging.KLogging
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.condition.DisabledForJreRange
import org.junit.jupiter.api.condition.DisabledIf
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariables
import org.junit.jupiter.api.condition.DisabledIfSystemProperties
import org.junit.jupiter.api.condition.DisabledIfSystemProperty
import org.junit.jupiter.api.condition.DisabledInNativeImage
import org.junit.jupiter.api.condition.DisabledOnJre
import org.junit.jupiter.api.condition.DisabledOnOs
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
Expand Down Expand Up @@ -713,13 +722,27 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
val methods = AnnotationSupport.findAnnotatedMethods(context.requiredTestClass, Pact::class.java,
HierarchyTraversalMode.TOP_DOWN)
if (executedFragments.size < methods.size) {
val nonExecutedMethods = (methods - executedFragments).filter {
!isAnnotated(it, Disabled::class.java)
val disabledAnnotations = listOf(
Disabled::class.java,
DisabledForJreRange::class.java,
DisabledIf::class.java,
DisabledIfEnvironmentVariable::class.java,
DisabledIfEnvironmentVariables::class.java,
DisabledIfSystemProperties::class.java,
DisabledIfSystemProperty::class.java,
DisabledInNativeImage::class.java,
DisabledOnJre::class.java,
DisabledOnOs::class.java
)

val nonExecutedMethods = (methods - executedFragments).filter { method ->
!disabledAnnotations.any { disabledAnnotation -> isAnnotated(method, disabledAnnotation) }
}.joinToString(", ") { it.declaringClass.simpleName + "." + it.name }
if (nonExecutedMethods.isNotEmpty()) {
throw AssertionError(
"The following methods annotated with @Pact were not executed during the test: $nonExecutedMethods" +
"\nIf these are currently a work in progress, add a @Disabled annotation to the method\n")
"\nIf these are currently a work in progress, add JUnit's @Disabled annotation " +
"(or one of the @DisabledIf/@DisabledFor/@DisabledOn annotations) to the method\n")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.apache.hc.client5.http.fluent.Request
import org.apache.hc.core5.http.ContentType
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIf
import org.junit.jupiter.api.extension.ExtendWith

@SuppressWarnings(['PublicInstanceField', 'JUnitPublicNonTestMethod', 'FactoryMethodName'])
Expand Down Expand Up @@ -153,4 +154,20 @@ class MultiTest {
.status(404)
.toPact()
}

@Pact(provider = 'multitest_provider', consumer = 'test_consumer')
@DisabledIf('shouldBeDisabled')
RequestResponsePact getAnotherUsersFragment3(PactDslWithProvider builder) {
builder
.uponReceiving('get all users')
.path('/idm/user')
.method('GET')
.willRespondWith()
.status(404)
.toPact()
}

boolean shouldBeDisabled() {
true
}
}

0 comments on commit e88957a

Please sign in to comment.