2015-03-26 4 views
0

Я пытаюсь издеваться над статическим методом void с PowerMock над Mockito, но он не работает так хорошо.PowerMock mockStatic не ловил насмешливый метод статического недействительного вызова

Мой пример кода:

BlackTempleTest.java

package com; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.invocation.InvocationOnMock; 
import org.mockito.stubbing.Answer; 
import org.powermock.api.mockito.PowerMockito; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import com.BlackTempleTest.Illidan; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ Illidan.class, EvilBrother.class }) 
public class BlackTempleTest { 

    Answer<Object> defaultAnswer = new Answer<Object>() { 
     @Override 
     public Object answer(InvocationOnMock invocation) throws Throwable { 
      System.out.println("Haha!"); 
      return null; 
     } 
    }; 

    @Test(expected = AssertionError.class) 
    public void testIllidanFight() throws Exception { 
     Illidan.startFight(); 
    } 

    @Test 
    public void testCheatOnIllidanFight() throws Exception { 
     PowerMockito.mockStatic(Illidan.class, defaultAnswer); 

     Illidan.startFight(); 
    } 

    @Test(expected = AssertionError.class) 
    public void testEvilBrotherFight() throws Exception { 
     EvilBrother.startFight(); 
    } 

    // dont work 
    @Test 
    public void testCheatOnEvilBrotherFight() throws Exception { 
     PowerMockito.mockStatic(EvilBrother.class, defaultAnswer); 

     EvilBrother.startFight(); 
    } 

    static class Illidan { 
     static void startFight() { 
      Assert.fail("You are not prepared!"); 
     } 
    } 
} 

EvilBrother.java

package com; 

import com.BlackTempleTest.Illidan; 

public class EvilBrother extends Illidan { 

} 

Моя проблема в том, что вложенный класс издевались, как ожидается, с комбинацией @PrepareForTest и PowerMockito.mockStatic, но если класс находится в собственном файле класса, эти утверждения не работают.

Как можно исправить этот тест?

Edit:

С

PowerMockito.doAnswer(defaultAnswer).when(EvilBrother.class, "startFight"); 

можно конвейерный плохой вызов через Powermock, однако Assert.fail выполняется.

java.lang.AssertionError: You are not prepared! 
    at org.junit.Assert.fail(Assert.java:88) 
    at com.BlackTempleTest$Illidan.startFight(BlackTempleTest.java:54) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873) 
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773) 
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:753) 
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466) 
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106) 
    at com.BlackTempleTest.testCheatOnEvilBrotherFight(BlackTempleTest.java:47) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

ответ

0

Решение, вы должны издеваться Иллидана вместо EvilBrother, даже если вы звоните EvilBrother.startFight, так как метод унаследован.

@Test 
public void testCheatOnEvilBrotherFight() throws Exception { 
    PowerMockito.mockStatic(Illidan.class, defaultAnswer); 

    EvilBrother.startFight(); 
} 
Смежные вопросы