2015-04-12 132 views
0

我想測試我的上傳 我USIG JUnit中,Mockmvc和Spring後多MockMvc春季測試

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:app-context.xml") 
@WebAppConfiguration 

public class UploadTest { 

@Autowired 
private WebApplicationContext wac; 

private MockMvc mockMvc; 

Application app; 
String session; 

@Before 
public void setup() throws Exception { 
    Users.init(); 
    Graphs.init(); 
    Sessions.init(); 
    this.mockMvc = MockMvcBuilders.standaloneSetup(new Controller()).build(); 
    Users.setConfig("dani.pass", "81dc9bdb52d04dc20036dbd8313ed055"); 
    MvcResult m = mockMvc.perform(get("/logIn?name=dani&encrypted=81dc9bdb52d04dc20036dbd8313ed055")) 
      .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8)) 
      .andExpect(jsonPath("$.status", is(1))) 
      .andExpect(jsonPath("$.error", is(""))) 
      .andReturn(); 
    String content = m.getResponse().getContentAsString(); 
    JSONObject jsonObject = new JSONObject(content); 
    session=jsonObject.get("data").toString(); 
} 

@Test 
public void uploadTest1() throws Exception { 
      String filePath = (new File(".")).getCanonicalFile().getCanonicalFile().getCanonicalPath() 
      + "/books.ttl"; 

      FileInputStream fis = new FileInputStream(filePath); 
      MockMultipartFile multipartFile = new MockMultipartFile("file", fis); 

      HashMap<String, String> contentTypeParams = new HashMap<String, String>(); 
      contentTypeParams.put("name", "http://exampleTest.com/ng"+Long.toString(System.currentTimeMillis())); 
      contentTypeParams.put("session", session); 
      MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams); 
      mockMvc.perform(MockMvcRequestBuilders.fileUpload("/upload") 
        .file(multipartFile) 
        .param("name", "http://exampleTest.com/ng"+Long.toString(System.currentTimeMillis())) 
        .param("session", session)) 
        .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8)) 
        .andExpect(jsonPath("$.status", is(1))) 
        .andReturn(); 
} 
} 

你能幫助我嗎?

錯誤堆棧跟蹤:

[org.springframewor[email protected]361cb7a1] 以製備測試實例[[email protected]] org.springframework。 beans.factory.BeanCreationException:錯誤 創建名爲'endpoint.security.tests.UploadTest'的bean: 注入自動裝配依賴失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:private com.sun.jersey.server.impl.application.WebApplicationContext endpoint.security.tests.UploadTest.wac;嵌套的異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 類型[com.sun.jersey.server.impl.application.WebApplicationContext]發現 爲依賴 排位豆:預期至少1豆,其有資格作爲自動裝配 此依賴關係的候選人。依賴註解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 [.cp /:NA]: org.springframework.beans.factory.BeanCreationException:致未能進行 autowire field:private com.sun.jersey.server.impl.application.WebApplicationContext endpoint.security.tests.UploadTest.wac;嵌套的異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 類型[com.sun.jersey.server.impl.application.WebApplicationContext]發現 爲依賴 排位豆:預期至少1豆,其有資格作爲自動裝配 此依賴關係的候選人。依賴註釋:

回答

0

從錯誤堆棧跟蹤看起來像WebApplicationContext是從錯誤的包com.sun.jersey.server.impl.application導入。

應該org.springframework.web.context

0

@Mithun得到了它的權利。你有錯誤的包進口WebApplicationContext