匿名Action类导致IndexOutOfBoundException

时间:2013-11-07 06:56:21

标签: java struts2 junit jmock

我在单元测试代码中有一些匿名的Action类。匿名类没有名称。 Class#getSimpleName返回""。初始化convention-plugin时会导致IndexOutOfBoundExceptionPackageBasedActionConfigBuilder#buildConfiguration跳过所有接口,枚举,注释和抽象类。它也应该跳过匿名类。将actionClass.isAnonymous()添加到跳过条件。 我的单元测试代码中有一些匿名的Action类。这不是好设计。

private PageAction action;

@Before 
public void beforeEach() {
    action = new PageAction() {};
}
@Test 
public void shouldAcceptAndPublicPageId() { 
    action.setPageId(1); 
    assertEquals(1, action.getRequestedPageId()); 
}
...
// To bypass complex logic in ViewPageAction
@Before 
public void beforeEach() {
    action = new ViewPageAction() {
        boolean isPageBookmarkedByUser(Page page, User user) { return true; }
        VisitPage visitPage() { return null; }
    };
    coreService = mockery.mock(CoreService.class);
    action.setCoreService(coreService);
    uiService = mockery.mock(UiService.class);
    action.setUiService(uiService);
    pageRepository = mockery.mock(PageRepository.class);
    action.setPageRepository(pageRepository);
    pageAttachmentRepository = mockery.mock(PageAttachmentRepository.class);
    action.setPageAttachmentRepository(pageAttachmentRepository);
    wiki = WikiTest.publicWiki();
    action.setWiki(wiki);
    User user = UserTest.FOO;
    action.setUser(user);
}
@Test 
public void success() {
    final Page page = PageTest.FOO_PAGE;
    final String text = "Content of the page";
    final PageRevision latestRevision = MockPageRevision.FOO_REV2;
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

对不起,这不是一个非常明确的问题,所以这是猜测:

您是否尝试将匿名ViewPageAction设置为适当的类?

// somewhere in your test class...
private static class DummyViewPageAction extends ViewPageAction {
    boolean isPageBookmarkedByUser(Page page, User user) { return true; }
    VisitPage visitPage() { return null; }
}

// start of your @Before method...
@Before
public void beforeEach() {
    action = new DummyViewPageAction();
    ...