2017-03-31 91 views
0

我有一个应用程序使用Spring MVC运行REST服务(没有Spring Boot)。上下文主要由父节点加载。 我有一个控制器,我想通过MockMVC进行测试。SpringBoot应用程序之外使用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 
    } 
} 

直至目前为止,测试控制器看起来是这样的:

@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 

并没有创建豆子

PS还有就是用

MockHttpServletRequest request = new MockHttpServletRequest(); 
MockHttpServletResponse response = new MockHttpServletResponse(); 

但是一个选项,它需要控制器方法的重构,这是不可能的

回答

1

原来,这是绝对有可能做到这一点。只有几个配置需要启动它。

  1. 您需要弹簧试验在pom.xml,使其工作

    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-test</artifactId> 
        <scope>test</scope> 
    </dependency> 
    
  2. 创建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 {  ... } 
    
  • 应该我提到,如果没有@ContextConfigurationMockMVC将无法​​正常工作。

  • @Before方法创建MockMVC实例:

    private MockMvc mockMvc; 
    
    @Mock 
    private Service mockService; 
    
    @Before 
    public void setup() { 
        MockitoAnnotations.initMocks(this); 
        mockMvc = MockMvcBuilders.standaloneSetup(new Controller(mockService)) 
          .setHandlerExceptionResolvers(exceptionResolver()) //crutial for standaloneSetup of MockMVC 
          .build(); 
    } 
    
  • 至于我理解,setHandlerExceptionResolversmockMVC设置的一个关键部分。

    基本上就是这样。

    相关问题