2017-08-09 53 views
0

我在Hello World上運行測試,試圖在我的spring java程序中關注Jersey Framework。filenotfoundexception運行jerseyTest - 缺少applicationContext.xml

我已經擴展了JerseyTest,但是我得到了上面的錯誤。澤西給出的例子link似乎沒有幫助。

public class SimpleTest extends JerseyTest { 



@Override 
protected Application configure() { 
    return new ResourceConfig(RestService.class); 
} 

@Test 
public void test() { 
    final String hello = target("\service\hello").request().get(HelloModel.class); 
    assertEquals("Hello World!", hello); 
} 
} 

回答

0

如果你有jersey-spring3依賴關係,就會發生這種情況。默認行爲是查找這個applicationContext.xml文件(這是您的Spring上下文配置)。

如果你想爲測試配置Spring,你可以做一些事情。

  1. 您可以手動創建ApplicationContext,如果你正在使用XML配置,那麼你將創建一個ClassPathXmlApplicationContext它傳遞給新澤西

    ApplicationContext context = ... 
    return new ResourceConfig(RestService.class) 
        .property("contextConfig", context) 
    

    。如果您使用的是Java配置,那麼您將創建一個AnnotationConfigApplicationContext

  2. 如果您需要servlet的servlet容器的支持,請在this post

+0

什麼會這樣的例子嗎? AnnotationConfigApplicationContext(YourSpringConfig.class)。我嘗試了應用程序和jerseyconfig,但他們沒有工作。 – user269964

+0

不完全確定你的意思。 –

+0

我去了2號的鏈接。我遵循了他們對所學課程的看法。我正在爲hello世界做.packages(com.service.rs)。我收到錯誤NoClassDefFound注入/ Binder – user269964