21

I'm a newbie to Java Unit test. The issue I'm facing is that I need to develop a JUnit test to send requests to a server'API to test two methods: addUser and deleteUser. If I want to add/delete a user from the server, I need to get an authentication token from the server; However, due to some issue on the server side, I currently can't get a valid token. So what comes to my mind, is to mock the server's behavior that if the server receives requests from the Unit test, it could response with a JSON data which indicates the status of the add/delete-user operations.

Because I'm totally new to JUnit. I have no clue how to implement the operation. So my question is what is probably the easiest way to apply the mock?

dpr
  • 10,591
  • 3
  • 41
  • 71
  • 2
    Mockito is the tool you should look for. There is a article about this http://stackoverflow.com/questions/22714359/how-to-partially-mock-httpservletrequest-using-mockito – codebee May 10 '17 at 18:32
  • 2
    Something similar has been answered before: http://stackoverflow.com/questions/606352/how-to-mock-a-web-server-for-unit-testing-in-java – liminal May 10 '17 at 18:51
  • 1
    What are you actually testing? You said you wanted to test two methods of the server API. Then you want to replace the server with a mock. If you do that, then you wouldn't be testing the server API any more. You really need to have it clear in your mind which parts of your system you want to test, and which parts you want to mock. – Dawood ibn Kareem May 10 '17 at 19:18
  • 2
    If you really want to test the part that sends HTTP messages to a server, it very much depends on what library you use. For example, if you are using Spring's `RestTemplate`, then you could use the Spring's mock server to mock the communication. Other libraries may require other tools. If you would tell us, what you are using and what you want to test, that would help to answer... – Florian Schaetz May 11 '17 at 06:03
  • @dopatraman requirement doesn't sound like unit test, but integration test, what you expect for bounty answer? – Ori Marko Dec 09 '19 at 13:07
  • @user7294900 true, but the question still needs a detailed answer. The ansers given are not detailed enough. For example, to mock `HttpResponse`, you need to mock 10 different methods. There must be an easier way, no? – dopatraman Dec 09 '19 at 18:12
  • @dopatraman I used mocking server, as postman – Ori Marko Dec 10 '19 at 05:55
  • @dopatraman Based on the requirements you might extend `BasicHttpResponse` and overwrite the methods you need to return some specific information. As user [@user5787623](https://stackoverflow.com/users/5787623/user5787623) does not exist anymore we cannot ask him what he wanted to achieve. – SubOptimal Dec 11 '19 at 06:53
  • @dopatraman I don't think this is a good question to have assigned to a bounty to. The user who asked this question two years ago has apparently moved on and solved the problem. The question itself is rather unclear and lacking details necessary for a high quality answer, unless someone just assumes a bunch of details and posts a full-on tutorial. You would have been better off asking your own question, preferably a clearer one. – DavidS Dec 12 '19 at 21:29
  • I'd ask for a code example. But given the OP is not active anymore, I guess odds are bad, to get something valid. In general the whole question sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). @dopatraman if you're so eager to get a valid/detailed answer, I'd recommend to post a new question with your requirements. Instead of adding a bounty to a question of disputable quality. – dpr Dec 13 '19 at 08:00

3 Answers3

8
HttpResponse httpResponse = mock(HttpResponse.class);

see this: Mocking Apache HTTPClient using Mockito

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
M2E67
  • 937
  • 7
  • 23
6

You can start a test-http-server in your unit-test. There are several frameworks to do it. For example Mockserver and Wiremock.

Felix
  • 2,256
  • 2
  • 15
  • 35
3

You can do it by using the HTTP Unit Testing from Google.

In order to generate a simple HTTP response you can use this code :

HttpTransport transport = new MockHttpTransport();
HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();

You can also customize your testing by overriding the implementation of the MockHttpTransport class.

You can check here the repo of this for more details. I think it can be suitable for you.

Andrei Tigau
  • 2,010
  • 1
  • 6
  • 17