简体   繁体   中英

Post multipart MockMvc Spring Tests

I am trying to test my upload I am usig Junit, Mockmvc and 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();
}
}

Could you help me?

Error Stack Trace:

[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@361cb7a1] to prepare test instance [endpoint.security.tests.UploadTest@6f7918f0] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpoint.security.tests.UploadTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sun.jersey.server.impl.application.WebApplicationContext endpoint.security.tests.UploadTest.wac; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sun.jersey.server.impl.application.WebApplicationContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at [.cp/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sun.jersey.server.impl.application.WebApplicationContext endpoint.security.tests.UploadTest.wac; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sun.jersey.server.impl.application.WebApplicationContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:

From the error stacktrace, looks like WebApplicationContext is imported from wrong package com.sun.jersey.server.impl.application .

It should be org.springframework.web.context

@Mithun has got it right. You have wrong package import for WebApplicationContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM