我正在尝试编写一个扩展另一个类的单元测试。我在另一个子类方法中调用超类方法:
儿童班
@RestController
@RequestMapping(PUBLIC_BASE_URL)
public class RequeteSASRestController extends BaseController {
private static final Logger LOGGER = LogManager.getLogger();
private final RequeteSASService requeteSASService;
@Inject
public RequeteSASRestController(
final RequeteSASService pRequeteSASJdbcService) {
requeteSASService = pRequeteSASJdbcService;
}
@ApiOperation(value = "Lecture d'une liste des requetes SAS", responseContainer = "List")
@RequestMapping(value = "/statistiques/requeteSAS", method = RequestMethod.GET)
public ResponseEntity<SimpleServiceResponse<List<RequeteSAS>>> readListeRequeteSas() {
LOGGER.debug(" readListeRequeteSas() ->");
String remoteUser=super.getCurrentUserLogin();
return this.ok(requeteSASService.readListeRequeteSas(remoteUser));
}
超类
public class BaseController {
public String getCurrentUserLogin() {
//Some things
}
最后这是我的测试课程:
public class RequeteSASRestTest extends Mockito{
private RequeteSAS requeteSASJdbc1;
private RequeteSAS requeteSASJdbc2;
private RequeteSAS requeteSASJdbc1suppr;
/** Service RequeteSAS */
private RequeteSASService mockRequeteService;
/** Rest Controller. */
private RequeteSASRestController restController;
// Before : mock des services
@Before
public void beforeTests() throws FileNotFoundException {
clearSecurityContext();
mockRequeteService = mock(RequeteSASService.class);
restController = Mockito.spy(new RequeteSASRestController(mockRequeteService));
requeteSASJdbc1 = RequeteSASTestUtil.createRequeteSAS(//instance constructor);
requeteSASJdbc2 = RequeteSASTestUtil.createRequeteSAS(//instance constructor);
}
@Test
public void testGetRequetes() {
String mockRemoteUser = "STATT-00016-0102035";
List<RequeteSAS> listReqTest = new ArrayList<RequeteSAS>();
listReqTest.add(requeteSASJdbc1);
listReqTest.add(requeteSASJdbc2);
Mockito.doReturn(listReqTest).when(mockRequeteService).readListeRequeteSas(mockRemoteUser);
Mockito.doReturn(mockRemoteUser).when((BaseController)restController).getCurrentUserLogin();
ResponseEntity<SimpleServiceResponse<List<RequeteSAS>>> repReq = restController.readListeRequeteSas();
List<RequeteSAS> listReq = repReq.getBody().getValue();
//Assert List
}
正如我所说,有了这个,仍然会调用BaseController中的方法“getCurrentUserLogin()”,它不会返回mockRemoteUser。我已经阅读了很多关于这个主题的线程,即模拟超类方法,我按照答案,但仍然我的测试失败,因为“getCurrentUserLogin()”返回null而不是模拟用户。
有人看到问题所在吗?谢谢。