我在 spring + testng + mockito + powermock 的单元测试中遇到问题
I have a problem in the unit test of spring + testng + mockito + powermock
我正在对spring项目的控制器进行单元测试,控制器代码如下::
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);
......
}
}
}
}
我用testng + mockito + powermock对controller.Becausecontroller extends BaseController进行了单元测试(代码如下),执行时会报Pageable p = getPageableFromRequest (map, "glt_msg", null);
NullPointerException。
基础控制器::
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;
}
}
测试class如下::
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();
}
}
The question I am confused about is :: how do I handle the getPageableFromRequest
method of BaseController
in that unit test.As we all know,the TestNG need to extends AbstractTestNGSpringContextTests
and the PowerMock need to extends PowerMockTestCase
. How should I combine these two tools for unit testing in spring projects
经过多次实验,发现SpringBootTest + TestNG + PowerMock的组合并没有work.The解决方法是:在TestNG测试的SpringBoot项目中,使用JMockit模拟静态方法,final方法等
我正在对spring项目的控制器进行单元测试,控制器代码如下::
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);
......
}
}
}
}
我用testng + mockito + powermock对controller.Becausecontroller extends BaseController进行了单元测试(代码如下),执行时会报Pageable p = getPageableFromRequest (map, "glt_msg", null);
NullPointerException。
基础控制器::
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;
}
}
测试class如下::
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();
}
}
The question I am confused about is :: how do I handle the
getPageableFromRequest
method ofBaseController
in that unit test.As we all know,the TestNG need to extendsAbstractTestNGSpringContextTests
and the PowerMock need to extendsPowerMockTestCase
. How should I combine these two tools for unit testing in spring projects
经过多次实验,发现SpringBootTest + TestNG + PowerMock的组合并没有work.The解决方法是:在TestNG测试的SpringBoot项目中,使用JMockit模拟静态方法,final方法等