我有一个应用程序使用 Spring MVC 来运行 REST 服务(没有 Spring Boot)。上下文主要是从父级加载的。
我有一个 Controller ,我想通过 MockMvc
测试它。
我曾尝试手动设置本地测试上下文,但这不足以运行测试。我想,应该还有我没有设置的额外 bean。
我的 Controller 是:
@RestController
public class ProrertyEditorController extends AbstractPropertyEditorController {
@Autowired
protected PropertyEditorService prorertyEditorService;
@RequestMapping(method = RequestMethod.DELETE, value = "/{dataType}/deletewithcontent")
@ResponseStatus(value = HttpStatus.OK)
public void deleteWithContent(@PathVariable("dataType") String dataType, @RequestParam("deleteall") boolean deleteAllContent, @RequestBody String node) {
try {
JSONArray itemsToDelete = new JSONArray(node);
prorertyEditorService.deleteItemsWithContent(dataType, itemsToDelete, deleteAllContent);
} catch (Exception e) {
//handling exception
}
}
}
到目前为止, Controller 的测试如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath*:configBeans1.xml")
public class ProrertyEditorControllerTest{
private MockMvc mockMvc;
@Mock
private PropertyEditorService mockService;
@InjectMocks
private ProrertyEditorController controller;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(new ProrertyEditorController()).build();
}
@Test
public void deleteWithContentTest() throws Exception {
mockMvc.perform(delete("/full/path/{dataType}/deletewithcontent", type)
.param("deleteall", "true")
.param("node", "[{\"test key1\":\"test value1\"}, {\"test keys2\":\"test value2\"}]"));
verify(mockService, times(1)).deleteItemsWithContent(eq("promotion"), eq(new JSONArray("[{\"test key1\":\"test value1\"}, {\"test keys2\": \"test value2\"}]")), eq(true));
}
}
不幸的是,由于 Failed to load ApplicationContext
并且没有创建 bean,它无法工作。
PS有一个选项可以使用
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
然而,它可能需要重构 Controller 方法。
最佳答案
事实证明,完全可以做到。启动它只需要一些配置。
您的
pom.xml
中需要spring-test
:<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency>
创建一个
testContext.xml
文件。就我而言,它实际上是空的:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
尽管如此,它仍然是必需的,否则
MockMVC
将不会启动,因为没有上下文。使用以下注释配置您的
ControllerTest
类:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:testContextConfig.xml") @WebAppConfiguration public class ControllerTest { ... }
如果没有
@ContextConfiguration
,MockMVC
将无法工作。在
@Before
方法中创建一个MockMVC
实例:private MockMvc mockMvc; @Mock private Service mockService; @Before public void setup() { MockitoAnnotations.initMocks(this); mockMvc = MockMvcBuilders.standaloneSetup(new Controller(mockService)) .setHandlerExceptionResolvers(exceptionResolver()) //crucial for standaloneSetup of MockMVC .build(); }
据我所知,
setHandlerExceptionResolvers
是mockMVC
设置的关键部分。
关于java - 在 SpringBoot 应用之外使用 MockMVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43145408/