Вопрос

I'm trying to use JMockit in order to mock a protected method of a class:

public class A {

    protected String say() {
         return "hi";
    }
}

public class B extends A {

     public String cry() {
          return "waaaa " + say();
     }
}

I want to mock "say" method in my tests, so that every instance of B, when it invokes "say", it will get "bye" instead of "hi".

Thanks.

Это было полезно?

Решение

You can simply make a MockUp of A:

new MockUp<A> () {
    @Mock protected String say() { return "bye"; }
};
System.out.println(new B().cry()); // prints waaaa bye
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top