pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 478 forks source link

NoClassDefFoundError: kotlin/reflect/full/KClasses #1356

Closed JohnCannon87 closed 3 years ago

JohnCannon87 commented 3 years ago

Hi,

Getting an issue when running a provider verification junit test with the error below coming back:

I've tried adding: ext['kotlin.version'] = '1.4.31' To my build.gradle but that hasn't helped, looks like some kind of transitive dependency issue but can't seem to spot anything and can't find anything in the docs, worth pointing out I get a different but similar error on versions other than 4.2.4 as well.

java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses at au.com.dius.pact.provider.ProviderUtils.findAllPactSources(ProviderUtils.kt:102) at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider.findPactSources(PactJUnit5VerificationProvider.kt:138) at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider.resolvePactSources(PactJUnit5VerificationProvider.kt:77) at au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider.provideTestTemplateInvocationContexts(PactJUnit5VerificationProvider.kt:43) at au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider.provideTestTemplateInvocationContexts(PactVerificationSpringProvider.kt:24) at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.lambda$execute$0(TestTemplateTestDescriptor.java:103) at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271) at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.execute(TestTemplateTestDescriptor.java:107) at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.execute(TestTemplateTestDescriptor.java:42) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185) at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185) at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185) at java.base/java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) Caused by: java.lang.ClassNotFoundException: kotlin.reflect.full.KClasses at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 51 more

This is my dependency list:


dependencies {
    compileOnly 'org.hibernate.validator:hibernate-validator:6.1.5.Final',
                'io.jsonwebtoken:jjwt-api:0.11.2',
                'org.projectlombok:lombok'

    implementation 'org.springframework.boot:spring-boot-starter-actuator',
                    'org.springframework.boot:spring-boot-starter-data-mongodb',
                    'org.springframework.boot:spring-boot-starter-thymeleaf',
                    'org.springframework.boot:spring-boot-starter-web'

    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor',
                        'org.projectlombok:lombok'

    testImplementation('org.junit.jupiter:junit-jupiter-api:5.7.1'){
        exclude group: 'org.mockito', module: 'mockito-core'
    }

    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.7.1'

    testImplementation 'org.awaitility:awaitility:4.0.3'

    testImplementation 'org.mockito:mockito-inline:3.4.6',
                       'org.mockito:mockito-junit-jupiter:3.4.6'

    testImplementation('org.mockito:mockito-core'){
        version {
            strictly '3.4.6'
        }
    }

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        exclude group: 'org.mockito', module: 'mockito-core'
    }

    testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo', 
                        'org.springframework.security:spring-security-test'

    sysTestImplementation 'io.rest-assured:rest-assured:4.3.3'
    sysTestImplementation 'io.rest-assured:rest-assured-common:4.3.3'
    sysTestImplementation 'io.rest-assured:xml-path:4.3.3'
    sysTestImplementation 'io.rest-assured:json-path:4.3.3'

    pactTestImplementation 'io.rest-assured:rest-assured:4.3.3'
    pactTestImplementation 'io.rest-assured:rest-assured-common:4.3.3'
    pactTestImplementation 'io.rest-assured:xml-path:4.3.3'
    pactTestImplementation 'io.rest-assured:json-path:4.3.3'
    pactTestImplementation 'au.com.dius.pact.consumer:junit5:4.2.4'
    pactTestImplementation 'au.com.dius.pact.provider:junit5spring:4.2.4'
    pactTestImplementation 'au.com.dius.pact.provider:junit5:4.2.4'

}

configurations.all {
 resolutionStrategy {
  force "org.mockito:mockito-core:3.4.6"
 }
 }

And this is my test itself:

import java.time.ZonedDateTime;
import java.util.Map;
import java.util.UUID;

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.StateChangeAction;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Provider("Example_Service_Provider")
@PactBroker(url = "http://localhost:9292")
class ExamplePactProvider {
//@formatter:off

    private static UUID KNOWN_UUID = UUID.fromString("e58d0b83-87df-44cc-a79e-b548f8b43046");
    private static ZonedDateTime createdDateTime = ZonedDateTime.now().minusDays(1);
    private static ZonedDateTime lastUpdatedDateTime = ZonedDateTime.now();

    @Autowired
    private ExampleObjectRepository exampleObjectRepository;

    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    void pactVerificationTestTemplate(final PactVerificationContext context) {
        context.verifyInteraction();
    }

    @State("CRUD Retrieval State")
    public void crudRetrievalState(final Map<String, Object> providerStateParameters) {
        ExampleObject exampleObject = ExampleObject.builder()
                .id(KNOWN_UUID)
                .name("Test Object")
                .createdAt(createdDateTime)
                .lastModifiedDate(lastUpdatedDateTime)
                .build();

        exampleObjectRepository.save(exampleObject);
    }

    @State(value = "CRUD Retrieval State", action = StateChangeAction.TEARDOWN)
    public void crudRetrievalStateCleanup(final Map<String, Object> providerStateParameters) {
        exampleObjectRepository.deleteById(KNOWN_UUID);
    }

//@formatter:on
}
JohnCannon87 commented 3 years ago

Sorry about this, appears to be an Eclipse thing, the test runs fine inside Gradle directly, I've added the dependencies below (had to keep adding as more missing classes came in) and it's working now, feel free to close if you think there's nothing that can be done to stop needing these to be added.

        compile 'org.jetbrains.kotlin:kotlin-reflect:1.4.31'
    // https://mvnrepository.com/artifact/org.apache.tika/tika-core
    implementation group: 'org.apache.tika', name: 'tika-core', version: '1.26'
    // https://mvnrepository.com/artifact/com.github.ajalt/mordant
    implementation group: 'com.github.ajalt', name: 'mordant', version: '1.2.1'
    // https://mvnrepository.com/artifact/com.github.mifmif/generex
    implementation group: 'com.github.mifmif', name: 'generex', version: '1.0.2'

    // https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
    implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
uglyog commented 3 years ago

Looks like Eclipse is not dealing with transitive dependencies from a Gradle project correctly. I don't use Eclipse, so can't help there.