Can Powermockito mock final method in non-final concrete class?

后端 未结 1 667
执念已碎
执念已碎 2020-11-30 10:39

Let\'s say I have a non-final concrete class with a final method like the one below.

public class ABC {
  public final String myMethod(){
      return \"test         


        
相关标签:
1条回答
  • 2020-11-30 11:00

    This works :

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ABC.class)
    public class ABCTest {
    
        @Test
        public void finalCouldBeMock() {
            final ABC abc = PowerMockito.mock(ABC.class);
            PowerMockito.when(abc.myMethod()).thenReturn("toto");
            assertEquals("toto", abc.myMethod());
        }
    }
    
    0 讨论(0)
提交回复
热议问题