[英]How can I unit-test javanica @HystrixCommand annotated methods?
[英]How can I unit-test a Jersey web application deployed on Tomcat?
我正在構建部署在 Tomcat 上的 Jersey Web 應用程序。 我很難理解如何對應用程序進行單元測試。
通過簡單地實例化我的測試中的類並調用它們的方法(這與 Jersey 或 Tomcat 無關),可以測試核心業務邏輯(非 Jersey 資源類)。
但是,單元測試 Jersey 資源類(即映射到 URL 的類)的正確方法是什么?
我需要為此運行Tomcat嗎? 或者我應該模擬請求和響應對象,在我的測試中實例化資源類,並將模擬提供給我的類?
我已經在 Jersey 的站點上閱讀了有關測試的信息,但是他們在示例中使用的是 Grizzly 而不是 Tomcat,這是不同的。
請解釋如何做到這一點。 歡迎使用示例代碼。
如果您只想進行單元測試,則無需啟動任何服務器。 如果您有一個服務(業務層)或任何其他注入,如UriInfo
和類似性質的東西,您可以模擬它。 一個非常流行的模擬框架是Mockito 。 下面是一個完整的例子
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Beside Jersey dependencies, you will need Mockito.
*
* <dependency>
* <groupId>org.mockito</groupId>
* <artifactId>mockito-core</artifactId>
* <version>1.10.19</version>
* </dependency>
*
* @author Paul Samsotha
*/
public class SomethingResourceUnitTest {
public static interface SomeService {
String getSomethingById(int id);
}
@Path("something")
public static class SomethingResource {
private final SomeService service;
@Inject
public SomethingResource(SomeService service) {
this.service = service;
}
@GET
@Path("{id}")
public Response getSomethingById(@PathParam("id") int id) {
String result = service.getSomethingById(id);
return Response.ok(result).build();
}
}
private SomethingResource resource;
private SomeService service;
@Before
public void setUp() {
service = Mockito.mock(SomeService.class);
resource = new SomethingResource(service);
}
@Test
public void testGetSomethingById() {
Mockito.when(service.getSomethingById(Mockito.anyInt())).thenReturn("Something");
Response response = resource.getSomethingById(1);
assertThat(response.getStatus(), is(200));
assertThat(response.getEntity(), instanceOf(String.class));
assertThat((String)response.getEntity(), is("Something"));
}
}
也可以看看:
如果您想運行集成測試,我個人認為運行 Grizzly 容器與運行 Tomcat 容器並沒有太大區別,只要您沒有在應用程序中使用任何特定於 Tomcat 的東西。
使用Jersey 測試框架是集成測試的一個不錯的選擇,但它們沒有 Tomcat 提供程序。 只有 Grizzly、In-Memory 和 Jetty。 如果您沒有使用任何 Servlet API,如HttpServletRequest
或ServletContext
等,則 In-Memory 提供程序可能是一個可行的解決方案。 它將為您提供更快的測試時間。
也可以看看:
如果必須使用 Tomcat,則可以運行自己的嵌入式 Tomcat。 我沒有找到太多文檔,但在 DZone 中有一個例子。 我並沒有真正使用嵌入式 Tomcat,但是按照上一個鏈接中的示例,您可以使用以下內容(已經過測試)
import java.io.File;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Aside from the Jersey dependencies, you will need the following
* Tomcat dependencies.
*
* <dependency>
* <groupId>org.apache.tomcat.embed</groupId>
* <artifactId>tomcat-embed-core</artifactId>
* <version>8.5.0</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.apache.tomcat.embed</groupId>
* <artifactId>tomcat-embed-logging-juli</artifactId>
* <version>8.5.0</version>
* <scope>test</scope>
* </dependency>
*
* See also https://dzone.com/articles/embedded-tomcat-minimal
*
* @author Paul Samsotha
*/
public class SomethingResourceTomcatIntegrationTest {
public static interface SomeService {
String getSomethingById(int id);
}
public static class SomeServiceImpl implements SomeService {
@Override
public String getSomethingById(int id) {
return "Something";
}
}
@Path("something")
public static class SomethingResource {
private final SomeService service;
@Inject
public SomethingResource(SomeService service) {
this.service = service;
}
@GET
@Path("{id}")
public Response getSomethingById(@PathParam("id") int id) {
String result = service.getSomethingById(id);
return Response.ok(result).build();
}
}
private Tomcat tomcat;
@Before
public void setUp() throws Exception {
tomcat = new Tomcat();
tomcat.setPort(8080);
final Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
final ResourceConfig config = new ResourceConfig(SomethingResource.class)
.register(new AbstractBinder() {
@Override
protected void configure() {
bind(SomeServiceImpl.class).to(SomeService.class);
}
});
Tomcat.addServlet(ctx, "jersey-test", new ServletContainer(config));
ctx.addServletMapping("/*", "jersey-test");
tomcat.start();
}
@After
public void tearDown() throws Exception {
tomcat.stop();
}
@Test
public void testGetSomethingById() {
final String baseUri = "http://localhost:8080";
final Response response = ClientBuilder.newClient()
.target(baseUri).path("something").path("1")
.request().get();
assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(String.class), is("Something"));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.