发布多部分MockMvc Sp​​ring Tests

时间:2015-04-12 11:55:42

标签: java spring junit multipartform-data mockmvc

我正在尝试测试我的上传内容 我是Junig,Mockmvc和Spring

@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.springframework.test.context.support.DependencyInjectionTestExecutionListener@361cb7a1]   准备测试实例[endpoint.security.tests.UploadTest@6f7918f0]   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; endpoint.security.tests.UploadTest&#39;的bean   注入自动连接的依赖项失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:私有   com.sun.jersey.server.impl.application.WebApplicationContext   endpoint.security.tests.UploadTest.wac;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   合格的bean类型   找到[com.sun.jersey.server.impl.application.WebApplicationContext]   对于依赖:预计至少有1个bean有资格作为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}     在   [.cp /:na]引起:   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:私有   com.sun.jersey.server.impl.application.WebApplicationContext   endpoint.security.tests.UploadTest.wac;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   合格的bean类型   找到[com.sun.jersey.server.impl.application.WebApplicationContext]   对于依赖:预计至少有1个bean有资格作为autowire   这种依赖的候选人。依赖注释:

2 个答案:

答案 0 :(得分:0)

从错误堆栈跟踪中,似乎WebApplicationContext从错误的包com.sun.jersey.server.impl.application导入。

应为org.springframework.web.context

答案 1 :(得分:0)

@Mithun说得对。您WebApplicationContext的包导入错误。