Gradle junit-tests are failing for the same exact code that works in main with same dependencies

Viewed 48

I am using gradle-2.8 version due to organisation restrictions.

I am trying to read YAML file (OpenAPI - swagger specification) using the following code :

new OpenAPIParser().readLocation(urlString, null, parseOptions).getOpenAPI();

which is present in both the main and test sourceSets as it is.

I have defined 2 configurations

  • swagConfig (for compile time dependencies)
  • swagRuntime (for runtime dependencies)

where swagRuntime consists of the following dependencies :

'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.1'
'com.fasterxml.jackson.core:jackson-databind:2.12.1'
'com.fasterxml.jackson.core:jackson-core:2.12.1'
'com.fasterxml.jackson.core:jackson-annotations:2.12.1'

and the build.gradle includes the following :

configurations {
    compile.extendsFrom swagConfig
    runtime.extendsFrom swagRuntime
    runtime {
        resolutionStrategy {
            force 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-databind:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-core:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-annotations:2.12.1'
        }
    }
    testRuntime {
        resolutionStrategy {
            force 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-databind:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-core:2.12.1'
            force 'com.fasterxml.jackson.core:jackson-annotations:2.12.1'
        }
    }
}

However, when I execute my main source-Set code, it works perfectly fine but when I run my test sourceSet based JUnit Test, I am running into failure :

java.lang.NoSuchMethodError: org.yaml.snakeyaml.events.ScalarEvent.<init>(Ljava/lang/String;Ljava/lang/String;Lorg/yaml/snakeyaml/events/ImplicitTuple;Ljava/lang/String;Lorg/yaml/snakeyaml/error/Mark;Lorg/yaml/snakeyaml/error/Mark;Lorg/yaml/snakeyaml/DumperOptions$ScalarStyle;)V
    at com.fasterxml.jackson.dataformat.yaml.YAMLGenerator._scalarEvent(YAMLGenerator.java:908)
    at com.fasterxml.jackson.dataformat.yaml.YAMLGenerator._writeScalar(YAMLGenerator.java:879)
    at com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.writeNull(YAMLGenerator.java:787)
    at com.fasterxml.jackson.databind.ser.std.NullSerializer.serialize(NullSerializer.java:30)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serializeNull(DefaultSerializerProvider.java:495)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:303)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1514)
    at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1215)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1085)
    at io.swagger.v3.core.util.Yaml.pretty(Yaml.java:23)
    at io.swagger.v3.parser.converter.SwaggerConverter.readResult(SwaggerConverter.java:113)
    at io.swagger.v3.parser.converter.SwaggerConverter.readLocation(SwaggerConverter.java:91)
    at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)

Debugging

After further debugging the stacktrace, my guess is that the jackson-dataformat-yaml artifact being used is somehow not 2.12.1 but 2.9 or less because in 2.9 the constructor of org.yaml.snakeyaml.events.ScalarEvent.<init> with that signature is not found (I could be wrong as well).

I have tried printing the

  • classpath
  • the testRuntime configuration and it's resolved artifacts.

and I see that the 2.12.1 version itself for jackson-dataformat-yaml is being picked up everywhere. But still I am not clear why the build (with junit) fails as explained above.

Could anyone help in debugging this further or any insight into why the test sourceSet for JUnits is not resolving the artifacts correctly.

Appreciate all inputs. Thanks.

0 Answers
Related