小编ryz*_*man的帖子

在 SpringBoot 应用程序之外使用 MockMVC

我有一个使用 Spring MVC 运行 REST 服务的应用程序(没有 Spring Boot)。上下文主要是从父母那里加载的。我有一个控制器,我想通过 MockMVC 测试它。

我尝试手动设置本地测试上下文,但这不足以运行测试。我想,应该还有我没有设置的额外豆子。

我的控制器是:

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,控制器的测试如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath*:configBeans1.xml")
public class ProrertyEditorControllerTest{
    private MockMvc mockMvc;

    @Mock
    private PropertyEditorService mockService;
    @InjectMocks
    private ProrertyEditorController …
Run Code Online (Sandbox Code Playgroud)

java rest spring-mvc mockito spring-test-mvc

4
推荐指数
1
解决办法
1640
查看次数

标签 统计

java ×1

mockito ×1

rest ×1

spring-mvc ×1

spring-test-mvc ×1