我在Spring + Testng + Mockito + Powermock的单元测试中遇到问题

时间:2019-12-26 02:19:33

标签: spring unit-testing testng powermock

我正在对spring项目的控制器进行单元测试,控制器代码如下:

line = "\\x74\\x65\\x73\\x74"
line = line.encode('utf-8').decode('unicode_escape')
# test

我使用testng + mockito + powermock对控制器进行了单元测试。由于控制器扩展了BaseController(代码如下),因此在执行package app.dnatask.controller; import ... @Slf4j @RestController @RequestMapping(value = "/API/scanresultconfigure") public class ScanResultConfigureController extends BaseController { @Autowired private ScanResultConfigureService scanResultConfigureService; @RequestMapping(value = "/queryScanResultList/{taskId}/{externalname}", method = RequestMethod.POST) public IBaseResult queryscanResultList(final HttpServletRequest request, @PathVariable final String taskId, @PathVariable final String externalname, @RequestBody Map map) throws Exception { return runController(new IControllRunner() { public void run(IOutResult or, CheckResult cr) throws Exception { Pageable p = getPageableFromRequest(map, "glt_msg", null); List list = scanResultConfigureService.findtitleConfigure(taskId, externalname, map); ...... } } } } 时将报告NullPointerException。

BaseController ::

Pageable p = getPageableFromRequest (map, "glt_msg", null);

测试类如下:

package app.frame.vendor.spring.springmvc.common.usercontext;

import ...

public abstract class BaseController extends BaseController_Web {
    public BaseController() {
    }

    protected Pageable getPageableFromRequest(Map map, String gltName, Sort sort) {
        ...
        return pageable;
    }
}
  

我困惑的问题是::在该单元测试中如何处理package app.dnatask.controller; import ... @WebAppConfiguration @ContextConfiguration(classes = {ScanResultConfigureController.class}) @ComponentScan( excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = { ComponentScan.class, Configuration.class, ImportResource.class }) }, useDefaultFilters = false, lazyInit = true ) @EnableWebMvc @PrepareForTest(BaseController.class) public class ScanResultConfigureControllerTest extends AbstractTestNGSpringContextTests { @MockBean(answer = Answers.RETURNS_DEEP_STUBS) private ScanResultConfigureService scanResultConfigureService; @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @BeforeMethod public void setup() { MockitoAnnotations.initMocks(this); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).dispatchOptions(true).build(); } @Test public void testQueryscanResultList() throws Exception { PowerMockito.mock(BaseController.class); when(BaseController.getPageableFromRequest).thenReturn(Pageable); Map<String, String> testMap = new HashMap<>(); testMap.put("key1", "value1"); testMap.put("key2", "value2"); String requestJson = JSONObject.toJSONString(testMap); List testList = new ArrayList(); testList.add("test1"); testList.add("test2"); when(scanResultConfigureService.findtitleConfigure(anyString(), anyString(), anyMap())).thenReturn(testList); MvcResult mvcResult = mockMvc.perform( post("/API/scanresultconfigure/queryScanResultList/{taskId}/{externalname}", "123", "abc") .contentType(MediaType.APPLICATION_JSON) .content(requestJson) ) .andExpect(status().isOk()) .andDo(print()) .andReturn(); } } 的{​​{1}}方法。众所周知,TestNG需要扩展getPageableFromRequest并PowerMock需要扩展BaseController。在春季项目中,应该如何结合使用这两种工具进行单元测试

1 个答案:

答案 0 :(得分:0)

经过多次实验发现,SpringBootTest + TestNG + PowerMock的组合不起作用,解决方法是:在TestNG测试的SpringBoot项目中,使用JMockit模拟静态方法,最终方法等