尝试测试我们用于多个文件上传的弹簧控制器。这是控制器:
@RequestMapping("/vocabularys")
@Controller
public class VocabularyController {
...
我要测试的动作:
@RequestMapping(value = "/import", method = {RequestMethod.PUT, RequestMethod.POST})
@ResponseBody
@CacheEvict(value="vocabulary", allEntries=true)
public Object importVocabulary(MultipartHttpServletRequest request, HttpServletResponse response) {
...
我在 webmvc-config.xml 中的解析器:
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>
代码工作得很好。当我尝试对此进行单元/集成测试时遇到了问题。
这是我的测试尝试:
public class VocabularyControllerTest extends BaseControllerTest {
static final private String AdminUsername = "administrator";
@Test
public void shouldBeAbleToUploadAFile() throws Exception {
createTestWorkspace();
login(AdminUsername, "*");
MockMultipartFile file = new MockMultipartFile("test_vocab.xml", new FileInputStream("src/test/files/acme_vocabulary.xml"));
MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = (MockMultipartHttpServletRequestBuilder) fileUpload("/vocabularys/import").accept(MediaType.ALL).session(httpSession);
mockMultipartHttpServletRequestBuilder.file(file);
mockMultipartHttpServletRequestBuilder.content("whatever");
ResultActions resultActions = mockMvc.perform(mockMultipartHttpServletRequestBuilder);
resultActions.andExpect(status().isFound());
}
}
忽略createWorkspace()
andlogin()
和东西 - 这些是为了通过一些安全过滤器。
的相关部分BaseControllerTest
:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(locations = {
"file:src/test/resources/META-INF/spring/applicationContext.xml",
"file:src/test/resources/META-INF/spring/applicationContext-security.xml",
"file:src/main/resources/META-INF/spring/applicationContext-database.xml",
"file:src/main/resources/META-INF/spring/applicationContext-activiti.xml",
"file:src/main/resources/META-INF/spring/applicationContext-cache.xml",
"file:src/main/resources/META-INF/spring/applicationContext-jms.xml",
"file:src/main/resources/META-INF/spring/applicationContext-mail.xml",
"file:src/main/resources/META-INF/spring/applicationContext-mongo.xml"}),
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/spring/webmvc-config.xml",
"file:src/test/webapp/WEB-INF/spring/applicationContext-filters.xml"})
})
@Transactional
public class BaseControllerTest extends BaseTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession httpSession;
@Autowired
MockServletContext servletContext;
@Autowired
OpenEntityManagerInViewFilter openEntityManagerInViewFilter;
@Autowired
HiddenHttpMethodFilter hiddenHttpMethodFilter;
@Autowired
CharacterEncodingFilter characterEncodingFilter;
@Autowired
SessionFilter sessionFilter;
@Autowired
WorkflowAsSessionFilter workflowAsSessionFilter;
@Autowired
FilterChainProxy springSecurityFilterChain;
@Autowired
RequestFilter requestFilter;
MockMvc mockMvc;
protected static final String TestFileDir = "src/test/files/";
@Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.addFilter(openEntityManagerInViewFilter, "/*")
.addFilter(hiddenHttpMethodFilter, "/*")
.addFilter(characterEncodingFilter, "/*")
.addFilter(sessionFilter, "/*")
.addFilter(workflowAsSessionFilter, "/*")
.addFilter(springSecurityFilterChain, "/*")
.addFilter(requestFilter, "/*")
.build();
servletContext.setContextPath("/");
Session session = Session.findBySessionId(httpSession.getId());
if (session == null) {
session = new Session();
session.setJsessionid(httpSession.getId());
session.persist();
}
}
...
问题是当我尝试调试它时,对象perform
上的操作mockMvc
永远不会影响我的控制器方法。我认为通过我们的安全过滤器是一个问题(这就是我拥有所有这些login
东西的原因),但我在词汇控制器中测试了其他操作,我能够很好地击中它们。
想法?想法?建议?