4

I have REST service, written in language different from Java. It have few dependencies from other REST services.

For example service under development and testing is A, other services are respectively B and C.

I want to run system test for A, some tests require B or/and C to be online and perform queries from A.

I wrote b-mock.featue and c-mock.feature to represent that services in mock.

Also I wrote some a-test-smth.feature files to run test against A

Is it possible to add some information into a-test-smth.feature to enable some mocks for concrete test?

Now I should run standalone karate.jar twice, first - for mocking. second - for run tests. That approach works, but, I can't ceck that:

  • some API calls to A not required B or C
  • can't emulate service B down or for example slow or incorrect response answer fetching

Thanks.

1 Answers1

5

Are you using Java ? If so then the best approach is to perform the set-up of your test in Java code. You can start 2 mocks for B and c and then start the main test for your service A. And at the end do clean-up if needed.

You can refer this as an example: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

Row 3 shows how you can start a mock and run a Karate test.

If you are not using Java and would like to use only the stand-alone JAR, it is actually possible using Java-interop and quite easy, I just tried it.

EDIT: This API is now built into Karate, so you don't need to write the extra JS code below: https://github.com/intuit/karate/tree/master/karate-netty#within-a-karate-test

(Obsolete)

First create this bit of JavaScript code that is smart enough to start a Karate mock:

function() {
  var Mock = Java.type('com.intuit.karate.netty.FeatureServer');
  var file = new java.io.File('src/test/java/mock/web/cats-mock.feature');
  var server = Mock.start(file, 0, false, null);
  return server.port;
}

And this is how it can look in the Background of your main Karate test. You can see how you can do some conditional logic if needed and you have plenty of ways to change things based on your environment.

Background:
    * def starter = read('start-mock.js')
    * def port = karate.env == 'mock' ? starter() : 8080
    * url 'http://localhost:' + port + '/cats'

Does this answer your question ? Let me know and I will add this trick to the documentation !

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Yes I am not using Java in project where I wish to use Karate DSL. Java-interop works fine, and provided solution worked as expected! Thanks a lot! There is one more thing, related to running test cases. Now(after I use JS for start mock before test), test didn't finished with exit code, it freezes, like in case when I run simple mock. And I need to abort it by hands. I wold like to use it in CI, so there is need of exit to be completed gracefully. I think that it could be handler called after scenarios, that closes mock. Is that possible? – Konstantin Malinin Oct 09 '18 at 06:17
  • 1
    @KonstantinMalinin you can quit a mock gracefully by a simple GET, please try and let me know: https://github.com/intuit/karate/tree/master/karate-netty#stopping - if that does not work, you can call `stop()` on the server reference – Peter Thomas Oct 09 '18 at 08:11