[英]How to test RESTful application?
I have an application example with a service:我有一个带有服务的应用程序示例:
RestApp.java应用程序
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/webapi")
public class RestApp extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
classes.add(MessageService.class);
return classes;
}
}
MessageService.java消息服务.java
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
@Stateless
@Path("/messages")
public class MessageService {
@Inject
private MessagesManager messagesManager;
@GET
@Path("all")
@Produces({MediaType.APPLICATION_JSON})
public List<Message> getMessages() {
return messagesManager.getMessages();
}
}
and the service depends on the singleton MessagesManager.java:并且服务依赖于单例 MessagesManager.java:
import javax.ejb.*;
import javax.inject.Singleton;
@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class MessagesManager implements Serializable {
private List<Message> messages = new ArrayList<>();
@Lock(LockType.READ)
public List<Message> getMessages() {
messages.add(new Message(1, "message text"));
return messages;
}
}
and this app works fine.这个应用程序运行良好。 But during the test occurs error of injection:
但是在测试过程中出现注入错误:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MessagesManager,parent=MessageService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1232089028)
org.glassfish.hk2.api.UnsatisfiedDependencyException: SystemInjecteeImpl(requiredType=MessagesManager,parent=MessageService,qualifiers={},position=-1,optional=false,self=false,unqualified=null, 1232089028)
Test code is:测试代码为:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import static org.junit.Assert.assertEquals;
public class RestAppTest extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(MessageService.class);
}
@Test
public void testGet() {
final Response response = target("messages/all").request().get();
assertEquals(200, response.getStatus());
}
}
Why it happens and how to fix it?为什么会发生以及如何解决?
The class MessagesManager is missing in an application context.应用程序上下文中缺少 MessagesManager 类。 Add the class to configure method like this:
将类添加到配置方法中,如下所示:
return new ResourceConfig(MessageService.class, MessagesManager.class);
You need couple of things你需要几件事
1> Well formed JSON structure for your REST API 1> 为您的 REST API 格式良好的 JSON 结构
2> Some kind of REST client such as advanced REST client for chrome, Mozilla etc which can be used as a plugin. 2> 某些类型的 REST 客户端,例如用于 chrome、Mozilla 等的高级 REST 客户端,可用作插件。 POSTMAN is also a useful tool
POSTMAN 也是一个有用的工具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.