Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionnal tests to JShellAPI ? #30

Closed
Alathreon opened this issue May 25, 2024 · 1 comment · Fixed by #50
Closed

Add functionnal tests to JShellAPI ? #30

Alathreon opened this issue May 25, 2024 · 1 comment · Fixed by #50

Comments

@Alathreon
Copy link
Contributor

While the wrapper is tested, the REST API isn't, which can cause some problems, because of the sheer number of moving pieces.
So adding tests that verify that the REST API works would be very useful.
Only problem, the REST API needs docker.

@firasrg
Copy link
Member

firasrg commented Sep 24, 2024

Hello folks! This is a recap about the work in my PR #50, it was a long journey 😄 ! While it has not been merged yet, I believe it is currently ready as all reviews have been resolved now.

Acknowledgment : I’d like to thank @Alathreon and @surajkumar who provided me with good hints before I start. Also for their trust and assistance during development and PR review process.

Work Done

  1. Test Dependencies & Gradle Setup : Verified necessary test dependencies within the build.gradle file of JShellAPI subproject.

  2. Gradle Test Setup : I created 2 new Gradle tasks under a new group named as "Docker" :

test {
    dependsOn taskBuildDockerImage
    finalizedBy taskRemoveDockerImage
}

👉 Currently, these tasks were integrated only for testing.

NOTE: Some Gradle knowledge was required here. Contributors unfamiliar with Gradle should consult Gradle's documentation before proceeding.

  1. Test Class Setup : I initially thought using @SpringBootTest alone was sufficient. However, I found it necessary to add @ContextConfiguration for proper context initialization.
@ContextConfiguration(classes = Main.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JShellApiTests {
   // class body
}
  1. Test Method Design : I used the a combination of @Test annotation with @DisplayName to provide a clear description about the test.
@Test
@DisplayName("More concise and explicit description about the test")
public void myTest() { }
  1. Dependency Conflict: upon launching the test, I encountered a conflict with the logback-classic dependency. After a quick search on StackOverflow, I resolved this by excluding it from the test dependencies in build.gradle:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
  exclude group: 'ch.qos.logback', module: 'logback-classic'
}
  1. Testing Process : I wrote the test logic using the WebTestClient API from Spring WebFlux, which proved simpler than MockMvc.
this.webTestClient.mutate()
            .responseTimeout(Duration.ofSeconds(appConfig.evalTimeoutSeconds()))
            .build()
            .post()
            .uri(endpoint)
            .bodyValue(codeInput)
            .exchange()
            .expectStatus()
            .isOk()
            .expectBody(JShellResult.class)
            .value((JShellResult evalResult) -> assertThat(evalResult).isNotNull())
            .returnResult()
            .getResponseBody();
  1. Evaluating Multiple Requests in 1 Test : After the initial PR, @Alathreon suggested performing at least 2 evaluations to ensure test robustness. I implemented this, simulating 2 API calls with the same session, reusing variables across calls. This approach ensures that the functionality works consistently across requests.

  2. Cleanup : Some final cleanup and optimization of the changed classes, ensuring everything is neat and functional.

Thanks for reading and see you in the next integration test !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants