문제

I need to use a mock who has a final method. So i use powermock but it does not work

class B {
    public final int nb() {
        return 4;
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(B.class)
public class Exemple extends TestCase {
    @Test
    public void test() { 
        B b = PowerMockito.mock(B.class);

        PowerMockito.when(b.nb()).thenReturn(5);

        final int actualState = b.nb();

        assertEquals(5, actualState);

    }   
}

if someone has a solution, thank you in advance

도움이 되었습니까?

해결책

Your example is fine, should work without any problem.

Tests Passed: 1 passed in 0,226 s

I have tested it with

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency> 

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.2</version>
</dependency>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top