是否能用设计模式来模拟服务器响应,以便在“模拟服务器”上开发和测试applet?

我正在开发一个Java Applet。applet需要向服务器发出Ajax请求,但我还没有编写服务器代码。

在服务器还没有准备好的时候,我是否可以使用一个设计模式来模拟服务器响应,以便可以在这个“模拟服务器”上开发和测试applet?好像那些关于如何实现模拟服务器的示例代码非常有用。

Although I'm not entirely sure if this suits your needs, but you can checkout a simple class I made for this purposes here. Here is some sample code:

//let's say that when you GET to /users?id=2 the server should return the user with id 2
//first start the server on your favourite port
MockHttpServer server = new MockHttpServer(portNum);
server.start();

//We will add a mock response for every request we will do, so in this case, just one mock response
server.enqueueResponse(Status.OK, "application/json,"{\"user_id\":15,\"name\":\"paul\"}");

//now we will use curl or whatever to make the GET
curl http://0.0.0.0:3000/users?id=2

// we get the request object
Request req = server.getRequest();
assertEquals(req.getMethod(),"GET");
assertEquals(req.getParams().get("id"),"2")

Its still a work in progress but you can get an idea by reading the code on github. Hope it helps :)