Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我是否可以仅使用powermock+;莫基托+;朱尼特?_Java_Unit Testing_Junit_Mockito_Powermock - Fatal编程技术网

Java 我是否可以仅使用powermock+;莫基托+;朱尼特?

Java 我是否可以仅使用powermock+;莫基托+;朱尼特?,java,unit-testing,junit,mockito,powermock,Java,Unit Testing,Junit,Mockito,Powermock,我正在使用Junit+Mockito+Powermock编写一个测试 我有一个类似以下的类,我想测试它: 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 meth

我正在使用Junit+Mockito+Powermock编写一个测试

我有一个类似以下的类,我想测试它:

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.
}
我只想模拟
method1
,但不想模拟
method2
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是否可以实现这一点

我的测试看起来可能不是很优雅,但在这里发布之前,我已经简化了我的用例


谢谢。

如果您想要在实际实现中保留的方法比需要模拟的方法多得多(尤其是在您的情况下只有一种),那么我会选择spy而不是mock:

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
之外的所有方法都将通过实际实现进行调用。

是的,可以使用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;
}

}

希望有帮助

新版本的Mockito可能会复制我们也可以模拟静态方法。有人知道如何仅使用JUnit 5+Mockito内联3.7.7来解决这个问题吗?请记住,如果mock是类且方法是静态的,则不能使用标准的doX().when(mock).method。请使用上述安装程序的版本。