2011-05-08 45 views
8

我們有一個擴展JerseyTest的測試,我們需要注入一個模擬服務。我們應該怎麼做?用Mocks進行單元測試澤西島

感謝

+0

新澤西對我一個新的。我認爲每個人都使用JUnit或TestNG。 – duffymo 2011-05-08 23:51:19

+0

擴展JerseyTest允許您添加常規JUnit @Test註釋,但我不知道如何在Spring配置中使用jersey-spring注入模擬服務。有什麼想法嗎? – alecswan 2011-05-09 20:09:15

+0

當真的只有靜態方法時,你真的需要注入mockito嗎? – Spoike 2011-05-12 06:58:17

回答

2

編輯:這種解決方案只能與新澤西州的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()); 
    } 
+1

我想他指向:http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/雖然我沒有得到它的工作。 – 2013-01-24 11:09:15

+0

沒錯,本教程將我引向這個解決方案,並且它工作正常。感謝精確度。 – 2013-01-24 12:00:23

+0

您是否願意詳細說明如何將'@ Autowired' bean注入測試類(例如,您是否註釋了一些'@ RunWith')?你能在這裏通過你的測試課的簡單例子嗎? – 2013-01-25 08:05:43