1

Where I am unit testing a REST function created in javax.ws.rs, and using target() for URL, as in https://stackoverflow.com/a/28726499/715269, and I don't know how to set the server part of the URL.

If I set it in hard way, the test will run only in one surrounding, on release server, for example. But normally, I run the application on any server, and API functions work on different servers, depending on where I had launch the application.

If I won't set any server for target:

String targetUrl = "/api/v1/documentTypes";
try (Response response = target(targetUrl).request().post(Entity.json(simpleJson))) {

And look for the URI in response, I see some server part set there (http://localhost:9998/), but I don't know where is it from, and this imitated server does not know the API function from targetUrl string.

How should I write this part of unit test, so as to be run universally?

I think that maybe overrode configure() method could help, but I am not sure.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • Not sure why you need to change the host and port. But you can override `URI getBaseUri()` in the `JerseyTest`. You can't use make up any address though. You need to use a bindable address or you will get an exception. With the `in-memory` test provider you can make up any address. But again, I don't really understand why this needs to be changed. – Paul Samsotha Apr 28 '18 at 13:49
  • @PaulSamsotha Thank you, I had found that I put the API test in the wrong test class. Thank you anyway - I can check the correctness of the used server URL by your function. – Gangnus Apr 28 '18 at 13:52

1 Answers1

0

I have found!

The test class extends from the JerseyTest class.

public class DocumentTypesTest extends JerseyTest {

That class has configure() method, that must be overrode:

@Override
protected Application configure() {
    return new ResourceConfig(DocumentTypes.class);
}

And the new overriden method should return config of the class where the tested API functions lie.

It could cause some misunderstanding: Your API function could be grouped by 'value=' from several classes. And you (as I did) can create the same test class for all API functions of the group. That won't work!. You should make separate test class for every API methods' class.

Gangnus
  • 24,044
  • 16
  • 90
  • 149