2017-03-31 45 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設置的一個關鍵部分。

    基本上就是這樣。

    相關問題