2017-10-31 9 views
3

私はJunit + Mockito + Powermockを使ってテストを書いています。いくつかの静的メソッドは、powermock + mockito + junitを使用してのみモックできますか?

public class MyUtils { 
    public static Object method1() {} //I want to mock this only 
    public static void method2() {} //I want to keep this as is during my test. 
    public static void method3() {} //I want to keep this as is during my test. 
} 

は私だけではなく、method1method2またはmethod3をモックとしたい:

は、私がテストしたい、次のようなクラスを持っています。

@RunWith(PowerMockRunner.class) 
@PrepareForTest(MyUtils.class) 
public class MyTest { 

    @Before 
    public void setUpBeforeClass() throws Exception { 
     PowerMockito.mockStatic(MyUtils.class); 
    } 

    @Test 
    public void test1() throws Exception { 
     when(MyUtils.method1()).thenReturn(something); 

     MyUtils.method3(); //method3 is getting mocked with an empty implementation by PowerMockito 
    } 

    ... 
} 

は、私はいくつかの嘲笑のメソッドといくつかを持つことができ、すなわち、彼らはテスト中に、元の実装を保つ嘲笑されていませんか?これはMockito + Powermockで可能ですか?

私のテストは非常にエレガントに見えないかもしれませんが、ここに投稿する前に私のユースケースを簡略化しました。

ありがとうございます。

+0

[PowerMockとTestNGを使用して1つの静的メソッドをモックする]の可能な複製(https://stackoverflow.com/questions/20398120/mock-a-single-static-method-using-powermock-and-testng) – Turing85

答えて

2

あなたが嘲笑する必要があるものよりも、あなたが実際の実装を維持したいの方法より多くの方法を持っている場合は(あなたの場合は特にその一つだけ)、私はスパイの代わりに、モックのために行くだろう:

import static org.powermock.api.mockito.PowerMockito.spy; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest(MyUtils.class) 
public class MyTest { 

    @Before 
    public void setUpBeforeClass() throws Exception { 
     spy(MyUtils.class); 
    } 

    @Test 
    public void test1() throws Exception { 
     doReturn(something).when(MyUtils.class, "method1"); 

     MyUtils.method3(); // this will be a real call 
    } 

    ... 
} 

method1を除くすべてのメソッドが実際の実装で呼び出されます。

+0

Keepあなたが標準のdoX()を使うことはできません。mockがクラスでメソッドが静的であるときは、(mock)。上記のようにセットアップのバージョンを使用してください。 –

3

はい、以下のようにPowermockとJUnitを使って静的メソッドを模擬することが可能である:

import static org.junit.Assert.*; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner;  
import static org.powermock.api.mockito.PowerMockito.*; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest(IDGenerator.class) 
public class UserDAOTest { 
@Test 
public void createShouldReturnAUserId() { 

    UserDAO dao = new UserDAO(); 

    mockStatic(IDGenerator.class); 
    when(IDGenerator.generateID()).thenReturn(1); 
    int result = dao.create(new User()); 
    assertEquals(1, result); 
    verifyStatic(); 
} 

} 


public final class IDGenerator { 

static int i; 

public static final int generateID() { 
    return i++; 
} 

} 


public class UserDAO { 

public int create(User user){ 
    int id = IDGenerator.generateID(); 
    //Save the user object to the db 
    return id; 

} 

} 



public class User { 
private int id; 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

} 

はそれが役に立てば幸い!

関連する問題