Java 模拟HTTP客户端请求的HTTP服务器超时
关于: ->有没有关于如何自动化上述单元测试用例的想法 更具体地说,如果HTTP客户端将5秒设置为超时,我希望服务器在10秒后发送响应。这将确保我的客户机由于超时而失败,从而使该场景自动化Java 模拟HTTP客户端请求的HTTP服务器超时,java,http,junit,mocking,timeout,Java,Http,Junit,Mocking,Timeout,关于: ->有没有关于如何自动化上述单元测试用例的想法 更具体地说,如果HTTP客户端将5秒设置为超时,我希望服务器在10秒后发送响应。这将确保我的客户机由于超时而失败,从而使该场景自动化 我非常感谢服务器端的psuedo代码(任何轻量级http服务器,如jetty或任何其他服务器都可以)。您不希望在单元测试中实际连接到真正的服务器。如果您想要实际连接到真正的服务器,从技术上讲,这是一个集成测试 因为您正在测试客户机代码,所以应该使用单元测试,这样就不需要连接到真正的服务器。相反,您可以使用模
我非常感谢服务器端的psuedo代码(任何轻量级http服务器,如jetty或任何其他服务器都可以)。您不希望在单元测试中实际连接到真正的服务器。如果您想要实际连接到真正的服务器,从技术上讲,这是一个集成测试 因为您正在测试客户机代码,所以应该使用单元测试,这样就不需要连接到真正的服务器。相反,您可以使用模拟对象来模拟与服务器的连接。这是非常棒的,因为如果你使用一个真正的服务器(比如连接失败的中间会话等),你可以模拟一些难以实现的条件。 使用mock进行单元测试也会使测试运行得更快,因为你不需要连接任何东西,所以没有I/O延迟 由于您链接到了另一个问题,我将使用该代码示例(为了清晰起见,在这里重新列出),我创建了一个名为
MyClass
的类,该类使用一个方法foo()
,该方法连接到URL,如果连接成功,则返回true或false。正如相关问题所述:
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;
}
}
}
我将使用它来创建模拟对象,因为它是比较流行的模拟对象库之一。另外,由于代码在foo方法中创建了一个新的URL对象(这不是最好的设计),我将使用这个库,它可以拦截对new
的调用。在实际的生产代码中,我建议使用依赖项注入或至少使用方法提取来创建工厂方法的URL
对象,以便您可以覆盖它以简化测试。但既然我坚持你的榜样,我就不会改变任何事情
以下是使用Mockito和Powermock测试超时的测试代码:
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);
}
}
这个测试在不到一秒钟的时间内运行,这比实际等待超过5秒钟的真正超时要快得多 这是一个关于mockito以及如何利用用例的漂亮解释。非常感谢dkatzel。@dkatzel正在使用testng框架编写相同的超时测试用例。但是我的模拟对象是空的,我得到了空指针异常code@Testpublic void testSocketExceptionEvents()引发异常{String url=“”;url mockURL=PowerMockito.mock(url.class);PowerMockito.whenNew(url.class)。带参数(url)。然后返回(mockURL);SocketTimeoutException expectedException=new-SocketTimeoutException();PowerMockito.when(mockURL.openConnection()).ThentThrow(expectedException);Sender-Sender=new-Sender();String input=“{”level\”:3,\“event\”:{“name\”:\“myevent\”};JSONObject-vent=new-JSONObject(输入);Assert.assertNotNull(Sender.send(vent));这里是关于使用Mockito和Spring来覆盖通过使用
@ActiveProfiles
和@Profile
注释激活的模拟bean的内容。我是Mockito的新手(相对而言,是Spring),所以这篇文章以及这个答案非常有用。谢谢!