gpt4 book ai didi

rest - 使用 Mocks 对 Jersey 进行单元测试

转载 作者:行者123 更新时间:2023-12-04 08:50:20 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

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

引用 http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/图书馆。

如果你使用 Spring,你可以扩展 AbstractSpringAwareJerseyTest 而不是 JerseyTest,并注入(inject)你需要的任何东西。

根据要求,一小段代码:

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());
}

关于rest - 使用 Mocks 对 Jersey 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5931167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com