Simulate HTTP server time for requesting an HTTP client

Regarding: HttpURLConnection timeout issue

-> Any idea on how to automate the unit test case for the above?

In particular, if the HTTP client set 5 seconds as a timeout, I want the server to send a response in 10 seconds. This would ensure the loss of my client due to a timeout and thus automation of this scenario.

I would appreciate psuedo code for the back end (any lightweight HTTP server like a jetty or any other is fine).

+5
source share
2 answers

You do not want to connect to a real server in the unit test. If you want to actually connect to a real server, this is technically an integration test.

Since you are testing client code, you should use unit test, so you do not need to connect to a real server. Instead, you can use layouts to simulate a server connection. This is really great because you can simulate conditions that would be difficult to achieve if you were using a real server (for example, the connection was not in the middle of the session, etc.).

Testing modules with mocks will also make tests run faster, since you don’t need to connect to anything so that there is no I / O delay.

Since you contacted another question, I will use this sample code (resold here for clarity). I created a class called MyClass with the foo() method, which connects to the URL and returns true or false if the connection is successful, Since the related question is:

 public class MyClass { private String url = "http://example.com"; public boolean foo(){ try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); con.setConnectTimeout(5000); //set timeout to 5 seconds return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (java.net.SocketTimeoutException e) { return false; } catch (java.io.IOException e) { return false; } } } 

I will use Mockito to create mock objects, as this is one of the most popular object mock libraries. In addition, since the code creates a new URL object in the foo method (which is not a better design), I will use the PowerMock library, which can intercept calls to new . In real production code, I recommend using dependency injection or at least the extraction method to create a URL object for the factory method so that you can redefine it to make testing easier. But since I am following your example, I will not change anything.

Here is the test code using Mockito and Powermock to check for timeouts:

 import java.net.HttpURLConnection; import java.net.SocketTimeoutException; import java.net.URL; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import static org.junit.Assert.*; @RunWith(PowerMockRunner.class) //This tells powermock that we will modify MyClass.class in this test //- needed for changing the call to new URL @PrepareForTest(MyClass.class) public class ConnectionTimeOutTest { String url = "http://example.com"; @Test public void timeout() throws Exception{ //create a mock URL and mock HttpURLConnection objects //that will be our simulated server URL mockURL = PowerMockito.mock(URL.class); HttpURLConnection mockConnection = PowerMockito.mock(HttpURLConnection.class); //powermock will intercept our call to new URL( url) //and return our mockURL object instead! PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(mockURL); //This tells our mockURL class to return our mockConnection object when our client //calls the open connection method PowerMockito.when(mockURL.openConnection()).thenReturn(mockConnection); //this is our exception to throw to simulate a timeout SocketTimeoutException expectedException = new SocketTimeoutException(); //tells our mockConnection to throw the timeout exception instead of returnig a response code PowerMockito.when(mockConnection.getResponseCode()).thenThrow(expectedException); //now we are ready to actually call the client code // cut = Class Under Test MyClass cut = new MyClass(); //our code should catch the timeoutexception and return false assertFalse(cut.foo()); // tells mockito to expect the given void methods calls //this will fail the test if the method wasn't called with these arguments //(for example, if you set the timeout to a different value) Mockito.verify(mockConnection).setRequestMethod("HEAD"); Mockito.verify(mockConnection).setConnectTimeout(5000); } } 

This test passes in less than a second, which is much faster than actually waiting more than 5 seconds for a real timeout!

+11
source
 @dkatzel am writing the same timeout testcase using testng framework. But my mock objects are null and am getting null pointer exception.below is my code @Test public void testSocketExceptionEvents() throws Exception{ String url="https://www.google.co.in/"; URL mockURL = PowerMockito.mock(URL.class); PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(mockURL); SocketTimeoutException expectedException = new SocketTimeoutException(); PowerMockito.when(mockURL.openConnection()).thenThrow(expectedException); Sender sender = new Sender(); String input="{\"level\":3,\"event\":{\"name\":\"myevent\"}}"; JSONObject vent = new JSONObject(input); Assert.assertNotNull(sender.send(vent)); 
-1
source

Source: https://habr.com/ru/post/1201109/


All Articles