1

在一个工作项目中,我遇到了一个真正令人讨厌的错误/功能。我找到了一个解决方案,但是我真的很好奇它为什么会发生。这一切都是因为该项目有一些用 unitils 而不是普通的旧 EasyMock 编写的遗留测试。考虑在两个测试类后面依次运行。

public class Test1{

    @TestedObject
    private MyService myService;    

    @InjectIntoByType protected Mock<MyRepository> myRepo;

    @Test
    public void aTest(){

        myService.doSomething();
        myRepo.assertNotInvoked().getById(EasyMock.anyLong());
    }               
}

public class Test2{
    private IMocksControl control;
    private MyOtherService myService;
    private MyOtherRepo myRepo;

    public Test2(){
        control = EasyMock.createControl();
        myRepo = control.createMock(MyOtherRepo.class);
        myService = new MyOtherService(myRepo);
    }

    @Test
    public void aTest(){
        myRepo.getById(5L);
        EasyMock.expectLastCall().andReturn(new MyObject(5L));

        control.replay()
            MyObject result = myService.doSomething();
        control.verify()
        Assert.assertEquals(5L,result.getId().longValue);
    }
}

当我分别运行两个测试时,它们运行良好。两个绿色都工作。但是,如果我在彼此之后立即运行它们,则第二次测试失败并出现 java.lang.IllegalStateException: x matchers expected, y 记录错误(x 和 y 当然是整数,因为这是一个假的例子,我没有实数)。

我通过将 assertNotInvoked 行更改为:myRepo.assertNotInvoked().getById(null);

这当然不是真正的解决方案,我只是规避了匹配器的使用,我想知道我是否只是没有用它破坏遗留测试用例......有没有人有足够的 EasyMock + Unitils 经验可以帮助我理解这个 ?我最好的“猜测”是 AssertNotInvoked 没有以 EasyMock.replay()、EasyMock.verify() 块正确结束......但我可能是错的。

4

0 回答 0