Can I mock a super class method call?

后端 未结 6 1359
旧巷少年郎
旧巷少年郎 2021-02-12 22:11

Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymo

6条回答
  •  无人共我
    2021-02-12 22:33

    Expanding on @Cem Catikkas answer, using JMockit 1.22:

    @Test
    public void barShouldCallSuperBar() {
        new MockUp() {
            @Mock
            public void bar() {
                barCalled = true;
                System.out.println("mocked bar");
            }
        };
    
        Foo foo = new Foo();
        foo.bar();
    
        Assert.assertTrue(mockBar.barCalled);
    }
    

    No need for the static class annotated with @MockClass, it is replaced by the MockUp class.

提交回复
热议问题