PowerMockRule与EasyMock NOT Moquito一起使用

时间:2014-01-10 08:05:17

标签: java junit easymock powermock

我正在进行遗留项目,使用EasyMock而无法选择包含Mockito,据说我在使用PowerMockRule进行单元测试时遇到了麻烦。我发现几乎所有关于如何利用规则的例子都是用Mockito制作的。我真的可以使用EasyMock来帮助解决这个问题。

@RunWith(UnitilsJUnit4TestClassRunner.class)
public class ModuleCloningServiceImplTest {

  @Rule
  public PowerMockRule rule = new PowerMockRule();
  //...

  @Test
  public void copyModuleToPages_success_customLayoutTileCopy_default_tile() {           
    ModuleType moduleType = getModuleType(ModuleTypeCode.COUPON, 2, 3);
    Module<CouponViewer> module = getModule(1L, moduleType);

    Page copyPage = getPage(1L, moduleType);
    PageLayout newPageLayout = DomainObjectBuilder.newPageLayout().build();
    newPageLayout.setCode(PageLayoutCode.CUSTOM);
    copyPage.setPageLayout(newPageLayout);
    List<Page> pages = Arrays.asList(copyPage);

    PowerMock.mockStatic(CustomPageLayoutUtils.class);
    CustomPageLayoutUtils.tileNumberExistsInCustomPageLayout(9, null);
    expectLastCall().andReturn(false);
    CustomPageLayoutUtils.getPreferredOrDefaultTile(copyPage, moduleType);
    expectLastCall().once().andReturn(2);

    PowerMock.replayAll();
    OperationReport operationReport = moduleCloningService.copyModuleToPages(module, pages, 9);
    PowerMock.verifyAll();

    checkCopySuccessInPages(pages);
    for (Page page : pages) {
      for (ModulePage<? extends JaxbModule> modulePage : page.getModulePages()) {
        Assert.assertEquals(Integer.valueOf(2), modulePage.getTileNumber());
      }
    }
    Assert.assertTrue(operationReport.isSuccesful());
  }

对CustompPageLayoutUtils的每次调用都是输入方法而不是使用mock。我在这段代码上尝试过几次,但这是我最后一次尝试。任何见解都会非常受欢迎。我希望还有其他人可以用来解决这个问题。

1 个答案:

答案 0 :(得分:0)

最后我不得不放弃并刚刚创建了另一个测试类

@RunWith(PowerMockRunner.class)
@PrepareForTest( { CustomPageLayoutUtils.class })
public class ModuleCloningServiceImplPowermockTest {

  private ModuleCloningServiceImpl moduleCloningService;

  private final CouponViewer moduleContent = new CouponViewer();

  private final PageLayout pageLayout = DomainObjectBuilder.newPageLayout().build();

  private Site site;

  private Product product;

  private CrudDao crudDao;

  private AuditLogger auditlogger;

  @Before
  public void init() {
    moduleCloningService = new ModuleCloningServiceImpl();
    crudDao = PowerMock.createMock(CrudDao.class);
    auditlogger = PowerMock.createMock(AuditLogger.class);
    ReflectionUtils.setByType(moduleCloningService, crudDao);
    ReflectionUtils.setByType(moduleCloningService, auditlogger);
    product = new Product(1L);
    site = DomainObjectBuilder.newSite().withId(1l).withProduct(product).build();

    PowerMock.mockStatic(CustomPageLayoutUtils.class);
  }  

  @Test
  public void copyModuleToPages_success_customLayoutTileCopy_default_tile() {


    ModuleType moduleType = getModuleType(ModuleTypeCode.COUPON, 2, 3);
    Module<CouponViewer> module = getModule(1L, moduleType);

    Page copyPage = getPage(1L, moduleType);
    PageLayout newPageLayout = DomainObjectBuilder.newPageLayout().build();
    newPageLayout.setCode(PageLayoutCode.CUSTOM);
    copyPage.setPageLayout(newPageLayout);
    List<Page> pages = Arrays.asList(copyPage);

    CustomPageLayoutUtils customPageLayoutUtils = PowerMock.createMock(CustomPageLayoutUtils.class);

    customPageLayoutUtils.tileNumberExistsInCustomPageLayout(9, null);
    expectLastCall().andReturn(false);
    customPageLayoutUtils.getPreferredOrDefaultTile(copyPage, moduleType);
    expectLastCall().andReturn(1);
    crudDao.save(EasyMock.anyObject());
    expectLastCall();
    auditlogger.audit(EasyMock.anyObject(AuditLogEntry.class));

    PowerMock.replayAll();
    OperationReport operationReport = moduleCloningService.copyModuleToPages(module, pages, 9);
    PowerMock.verifyAll();

    checkCopySuccessInPages(pages);
    for (Page page : pages) {
      for (ModulePage<? extends JaxbModule> modulePage : page.getModulePages()) {
        Assert.assertEquals(Integer.valueOf(1), modulePage.getTileNumber());
      }
    }
    Assert.assertTrue(operationReport.isSuccesful());
  }
}

它有效,但它并不完美,感谢那些试图帮助我的人!