如何使用Mockito或任何其他相关的java框架来模拟超类方法

siv3szwd  于 2022-11-08  发布在  其他
关注(0)|答案(4)|浏览(222)

我的场景如下

class SuperClass{
   public void run(){
      System.out.println("I am running in Super class");
   }
}

class ChildClass extends SuperClass{
  public void childRunner(){
     System.out.println("Step 1");
     System.out.println("Step 2");
   **run();**
     System.out.println("Last Step");
  }
}

现在我想测试ChildClasschildRunner()方法,因为这个方法在内部调用超类方法,我需要一些关于如何模拟SuperClass中存在的run()方法的帮助/代码。

ibrsph3r

ibrsph3r1#

理想情况下,您应该“更喜欢组合而不是继承”。
如果你没有这个选项,你可以使用doNothing,当mock/spy对象中的方法被调用时,它告诉Mockito什么都不做。
下面的代码示例应该会有所帮助

@Test
public void tst() {
    ChildClass ch = Mockito.spy(new ChildClass());
    Mockito.doNothing().when((SuperClass)ch).run();
    ch.childRunner();

}

class SuperClass{
    public void run(){
        System.out.println("I am running in Super class");
    }
}

class ChildClass extends SuperClass{
    public void childRunner(){
        System.out.println("Step 1");
        run();
        System.out.println("Last Step");
    }
}

输出:

Step 1
Last Step

如果您使用super.run(),则此方法无效

qlfbtfca

qlfbtfca2#

下面是一个类的例子,它扩展了另一个类,并且它有一些其他的依赖项。在这个例子中,我将把超类调用移到另一个方法中,然后模拟超类调用方方法。

class Child extends Parent {

  @Autowired
  private Dependicy d;

  public Authentication authenticate(Authentication auth) {
    the code to be tested...
    superAuthenticate(auth);// the code that I don't want to deal with it.
    return auth;
  }

  protected Authentication superAuthenticate(Authentication auth) {
    return super.authenticate(auth);
  }
}

正如你在上面看到的,authenticate方法执行一些逻辑,然后调用超类的方法,所以我想模拟超类调用并测试我自己的代码块。下面是我的测试类:

@RunWith(MockitoJUnitRunner.class)
public class ChildTest {
    @Mock
    private Dependicy d;
    @InjectMocks
    private Child child = new Child();

    @Test
    public void testSomething() {
        Child spy = Mockito.spy(child);

        when(d.aMethod(aParam)).thenReturn(aResult);
        doReturn(usefulResult).when(spy).superAuthenticate(any());

        Authentication result = spy.authenticate(auth);
        assertThat(result).isNotNull;
    }
}
hwazgwia

hwazgwia3#

我已经在这里回答了取消超类方法的问题,但是根据你的问题,我更新了下面的代码
我发现了一种使用PowerMockito来抑制超类方法的方法。
1.使用PowerMockito.suppress方法和MemberMatcher.methodsDeclaredIn方法来隐藏父类方法
1.第二个在@PrepareForTest中添加父类
1.使用PowerMock运行您的测试类,即在您的测试类之上添加**@RunWith(PowerMockRunner.class)**。

@RunWith(PowerMockRunner.class)
@PrepareForTest({SuperClass.class})
public class TestChildClass(){

    @Spy
    private ChildClass testChildClassObj = Mockito.spy(new ChildClass());

    @Test
    public void testChildRunner(){
        PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(SuperClass.class));

        //your further test code

        testChildClassObj.childRunner();
    }
}

注意:只有当超类方法不返回任何内容时,这才有效。

isr3a4wc

isr3a4wc4#

作为一种快速的方法,我只是在调用super的run的子类上添加了一个不同的代理方法,然后您就可以模拟“代理”了。

相关问题