question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException at running JUnit-Test with Annocation @QuarkusTest

See original GitHub issue

Describe the bug

I have a junit parameterized test (written in Kotlin) that is annotated as a @QuarkusTest. My IDE can compile my test, but at the runtime i get a ClassCastException:

java.lang.ClassCastException: class io.restassured.internal.ValidatableResponseImpl cannot be cast to class io.restassured.response.ValidatableResponse (io.restassured.internal.ValidatableResponseImpl is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @74960bfa; io.restassured.response.ValidatableResponse is in unnamed module of loader 'app')

My Test Class:

@QuarkusTest
@QuarkusTestResource(TestResource::class)
class ResourcesTest {

    @ParameterizedTest(name = "{0}")
    @MethodSource("validData")
    fun `should succeed when input valid`(requestBody: String, responseBodyValidator: (resp: ValidatableResponse) -> Unit) {
        val assertThat = given()
            .config(
                RestAssured.config()
                    .encoderConfig(encoderConfig().encodeContentTypeAs("application/graphql", ContentType.TEXT))
            )
            .body(requestBody)
            .headers(mapOf(HttpHeader.USERNAME to "foobar", HttpHeader.TENANT_ID to "1"))
            .contentType("application/graphql")
            .post("/graphql")
            .then()
            .assertThat()
        assertThat
            .contentType(ContentType.JSON)
            .statusCode(200)
        responseBodyValidator.invoke(assertThat)
    }

    companion object {
        @JvmStatic
        fun validData(): List<Arguments> {
            val parameters = ArrayList<Arguments>()
            parameters.add(
                Arguments.of(
                    VALID_INSERT_HUMAN_REQUEST,
                    fun(resp: ValidatableResponse): Unit { resp.body("data.insertHumanResource.firstName", Matchers.`is`(FIRST_NAME)) })
            )
            return parameters
        }
    }
}

Expected behavior

No response

Actual behavior

No response

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

openjdk version “11.0.10” 2021-01-19 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.2.3.Final

Build tool (ie. output of mvnw --version or gradlew --version)

apache maven 3.8.1

Additional information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartwdouglascommented, Nov 22, 2021

This is a known issue with parameterised tests.

Because JUnit does not directly have support alternate class loaders we have to basically proxy the requests into a new class loader, so for parameterized tests we attempt to clone the parameters into the new class loader, which is not always possible.

If https://github.com/junit-team/junit5/issues/2579 is resolved we should be able to fix this.

0reactions
foalcommented, Nov 23, 2021

If somebody will face the same issue with @MethodSource I found the workaround:

instead of @MethodSource

	private static Stream<Arguments> testCreate() {
		return Stream.of(
			Arguments.of(getIngress(NAME_ISTIO, TEST_NAMESPACE, true), NEW, NEW),
			Arguments.of(getIngress(NAME_ISTIO, TEST_NAMESPACE, true), NEW_GW, NEW));
	}

	@ParameterizedTest
	@MethodSource
	@DisplayName("Should create Istio GW and VS for provided ingress")
	void testCreate(Ingress testIngress, BiPredicate<? super Ingress, ? super Gateway> testGatevay, BiPredicate<? super Ingress, ? super VirtualService> testVirtualService) {
        //test body
       }

I use the @EnumSource

	@Getter
	@AllArgsConstructor
	private static enum Params {
		SET01(getIngress(NAME_ISTIO, TEST_NAMESPACE, true), NEW, NEW),
		SET02(getIngress(NAME_ISTIO, TEST_NAMESPACE, true), NEW_GW, NEW),
		;

		private Ingress testIngress;
		private BiPredicate<? super Ingress, ? super Gateway> testGatevay;
		private BiPredicate<? super Ingress, ? super VirtualService> testVirtualService;
	}

	@ParameterizedTest
	@EnumSource
	@DisplayName("Should create Istio GW and VS for provided ingress")
	void testCreate(Params testData) {

		var testIngressIstio = testData.getTestIngress();
		var testGatevay = testData.getTestGatevay();
		var testVirtualService = testData.getTestVirtualService();
               //test body
      }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Your Application - Quarkus
It is possible to read annotations from the test class or method to control what the callback shall be doing. While it is...
Read more >
QuarkusTest-Annotation + Mockito Result in ClassLoader Errors
org.junit.jupiter.api.extension.TestInstantiationException: Failed to create test instance at io.quarkus.test.junit.QuarkusTestExtension.
Read more >
2020 quarkus-fascicle-understanding-v1.pdf
Here, we use the @QuarkusTest annotation to let Quarkus test the Author REST resource. We target the URL /authors with an HTTP GET...
Read more >
Explanation of ClassCastException in Java - Baeldung
In the case of explicit casting, it is highly recommended to check the compatibility of the types before attempting to cast using instanceof....
Read more >
JUnit @Ignore Test Annotation with Example - Guru99
To ignore a test, JUnit provides @Ignore annotation to disable the test. Sometimes you may not to execute test case because coding is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found