1

I have my junit tests for my spring api project in src/test/java. For testing the controllers, I have created a package com.example.controller, inside which I have my karate feature file sample.feature:

Feature: sample

  Scenario: Test Create
    Given path '/test'
    Given url 'https://localhost:8443'
    When method GET
    Then status 200

Below is my Test class:

package com.example.controller;

import com.intuit.karate.junit5.Karate;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ControllerTest {

  @Karate.Test
  Karate testSample() {
    return Karate.run("sample").relativeTo(getClass());
  }

}

For the mentioned setup, I am getting the following error:

org.opentest4j.AssertionFailedError: http call failed after 105 milliseconds for url: https://localhost:8443/test

For execution, I am using mvn clean test -Dtest=ControllerTest

I have karate-core:1.0.1 and karate-junit5:1.0.1 in my pom.xml, along with junit exclusions to the karate package. I want to run the karate feature file while my application is running, similar to how other junits are executed. Anything which I might be missing / incorrectly configured in such scenario?

Reference link from Karate Project.

Yash Patil
  • 84
  • 8

1 Answers1

1

I don't think @Karate.Test is compatible with @SpringBootTest so you may need to figure this out on your own.

Here's a tip - the Karate JUnit support is just a convenience which is not mandatory to use. You may be able to create reasonable test suites that play well with @SpringBootTest using the Karate "core" Runner class.

For a detailed explanation, see: https://stackoverflow.com/a/65578167/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Got it @PeterThomas. Thanks. I think usage of ```Runner``` should help me. – Yash Patil Sep 07 '21 at 10:22
  • I tried using ```Runner```, keeping my ```baseUrl``` as ```localhost```, worked for me. So if anyone wants to have their karate tests run along with their other regular junits, it is possible by just putting the ```@SpringBootTest(classes = KarateTestRunner.class)``` and the tests run as if a regular junit. This can be helpful if there are multiple environments, multiple URLs. Instead of having URLs switched wrt ```karate.env```, use this approach. – Yash Patil Sep 08 '21 at 06:58