Java 为自定义注释编写单元测试?

Java 为自定义注释编写单元测试?,java,spring,unit-testing,annotations,spring-annotations,Java,Spring,Unit Testing,Annotations,Spring Annotations,我有一个Java Spring方法,我想测试它,看起来像这样: @RunWithPermission(values={Permission.Manage, Permissions.ManageAll}, any=true) @RequestMapping(method = RequestMethod.POST) public Object createReport(@RequestBody ReportParam definition) throws Exception { ...

我有一个Java Spring方法,我想测试它,看起来像这样:

@RunWithPermission(values={Permission.Manage, Permissions.ManageAll}, any=true)
@RequestMapping(method = RequestMethod.POST)
public Object createReport(@RequestBody ReportParam definition) throws Exception {
    ...
    return reportManager.createReport(definition);
}
@Autowired
@InjectMocks
private RestReportController controller;
...
@Test()
@RunWithPermission(values={Permissions.View}, any=true)
public void testCreateReportPermissions() throws Exception {
    ReportParam param = getReportParam("report_param.json");
    param.setLabelIds(Arrays.asList(labelService.getHomeId()));
    controller.createReport(param);
}
现在,仅当用户具有
权限时,我的函数才会运行。管理
权限。管理所有
权限,但如果用户具有类似
权限的权限,则我的函数不会运行。查看
。我想写一个单元测试来测试这一点,所以我尝试了以下方法:

@RunWithPermission(values={Permission.Manage, Permissions.ManageAll}, any=true)
@RequestMapping(method = RequestMethod.POST)
public Object createReport(@RequestBody ReportParam definition) throws Exception {
    ...
    return reportManager.createReport(definition);
}
@Autowired
@InjectMocks
private RestReportController controller;
...
@Test()
@RunWithPermission(values={Permissions.View}, any=true)
public void testCreateReportPermissions() throws Exception {
    ReportParam param = getReportParam("report_param.json");
    param.setLabelIds(Arrays.asList(labelService.getHomeId()));
    controller.createReport(param);
}
编辑:尝试2:我还尝试创建某种类型的假用户会话,以查看是否可以在测试期间“设置”权限,但这似乎也不起作用

@Test()
public void testCreateReportPermissions() throws Exception{
    ReportParam param = getReportParam("report_param.json");
    param.setLabelIds(Arrays.asList(labelService.getHomeId()));
    sessionManager.clearUserSession();
    userSession = TestUtils.createFakeSession("testuser001", "1000", Permissions.View);
    sessionManager.setUserSession(userSession);
    controller.createReport(param);
}
然而,正如我的测试所见,它创建了报告,单元测试完成。但是,有没有办法让它抛出某种错误或异常,因为我用
permission.View
的权限注释了该方法?如果有人能帮忙,那就太好了。谢谢