单元测试泽西与模拟

ale*_*wan 8 rest jersey mockito

我们有一个扩展JerseyTest的测试,我们需要注入一个模拟服务.我们应该怎么做?

谢谢

Sam*_*CHI 2

编辑:此解决方案仅适用于 Jersey 1.x,因此已经过时了。

它指的是http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/库。

如果您使用 Spring,您可以扩展 AbstractSpringAwareJerseyTest 而不是 JerseyTest,并注入您需要的任何内容。

根据要求,一小段代码:

public class MyClassTest extends AbstractSpringAwareJerseyTest{

  @Autowired
  private LdapSetupAndTearDown ldapSetupAndTearDown;

  @Before
  public void setUp2() throws Exception {
    ldapSetupAndTearDown.setUp();
  }

  @After
  public void tearDown2() throws Exception {
    ldapSetupAndTearDown.tearDown();
  }

  public MyClassTest () throws Exception {
    super(new WebAppDescriptor.Builder()
    .contextPath("JSONUserServiceTest")
    .contextParam("contextConfigLocation",
        "classpath:/ctx-config-test.xml,classpath:/ctx-core.xml, classpath:/ctx-jmx-test.xml, classpath:ctx-jersey.xml, classpath:ctx-ldap.xml, classpath:ctx-ldap-test.xml")
    .servletClass(SpringServlet.class).contextListenerClass(ContextLoaderListener.class).build());
  }
Run Code Online (Sandbox Code Playgroud)