相关疑难解决方法(0)

如何在java中模拟单个方法

是否可以模拟Java类的单个方法?

例如:

class A {
    long method1();
    String method2();
    int method3();
}


// in some other class
class B {
    void someMethod(A a) {
       // how would I mock A.method1(...) such that a.method1() returns a value of my
       // choosing;
       // whilst leaving a.method2() and a.method3() untouched.
    }
}
Run Code Online (Sandbox Code Playgroud)

java mocking

30
推荐指数
3
解决办法
5万
查看次数

使用PowerMock模拟私有方法,但仍会调用基础方法

我试图嘲笑模拟一个正在进行JNDI调用的私有方法.当从单元测试调用该方法时,它会抛出异常^.我想嘲笑这种方法用于测试目的.我使用了来自另一个问题答案示例代码,并且在测试通过时,似乎仍然调用了基础方法.我System.err.println()doTheGamble()方法中插入了一个,然后打印到我的控制台.

有趣的是,如果我将第一个注释掉assertThat,测试就会通过.?:(

那么,我如何模拟私有方法,以便它不被调用?

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

import java.util.Random;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class PowerMock_Test {

    static boolean gambleCalled = false; 

    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());

        when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt())
                .thenReturn(true);

/* 1 */ assertThat( …
Run Code Online (Sandbox Code Playgroud)

java junit mockito powermock

27
推荐指数
1
解决办法
6万
查看次数

如何验证未抛出异常

在我使用Mockito的单元测试中,我想验证NullPointerException没有被抛出.

public void testNPENotThrown{
    Calling calling= Mock(Calling.class);
    testClass.setInner(calling);
    testClass.setThrow(true);

    testClass.testMethod();

    verify(calling, never()).method();
}
Run Code Online (Sandbox Code Playgroud)

我的测试设置了testClass,设置Calling对象和属性,以便该方法将抛出一个NullPointerException.

验证从不调用Calling.method().

public void testMethod(){
    if(throw) {
        throw new NullPointerException();
    }

    calling.method();
}
Run Code Online (Sandbox Code Playgroud)

我想要一个失败的测试,因为它抛出一个NullPointerException,然后我想写一些代码来解决这个问题.

我注意到的是测试总是通过,因为异常永远不会被测试方法抛出.

java mocking mockito powermock

16
推荐指数
2
解决办法
3万
查看次数

在正在测试的同一个类中模拟私有方法

我有一个名为的Java类MyClass,我想用JUnit进行测试.methodA我要测试的公共方法调用私有方法methodB,在同一个类中确定要遵循的条件路径.我的目标是为不同的路径编写JUnit测试methodA.此外,methodB调用服务,所以我不希望它在运行JUnit测试时实际执行.

模拟methodB和控制其返回的最佳方法是什么,以便我可以测试'methodA'的不同路径?

我更喜欢在编写模拟时使用JMockit,所以我对任何适用于JMockit的答案都特别感兴趣.

这是我的示例类:

public class MyClass  {

    public String methodA(CustomObject object1, CustomObject object2)  {

        if(methodB(object1, object2))  {
            // Do something.
            return "Result";
        }

        // Do something different.
        return "Different Result";

    }

    private boolean methodB(CustomObject custObject1, CustomObject custObject2)  {

        /* For the sake of this example, assume the CustomObject.getSomething()
         * method makes a service call and therefore is placed in this separate
         * method so that later an …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing jmockit mocking

14
推荐指数
2
解决办法
4万
查看次数

如何在Mockito中测试私有的类方法

假设我们有一个名为SomeClass的java类

public class SomeClass {

    private boolean isMethod() {

        return false;
    }

    public void sendRequest(String json, String text) {

        int messageId;

        if (isMethod()) {
            messageId = getMessageId(json);
            sendMessage(messageId, text);
        } else {
            throw new IllegalArgumentException();
        }
    }

    private void sendMessage(int messageId, String text) {

    }

    private int getMessageId(String text) {

        Pattern p = Pattern.compile("messageId=(\\d+)&");
        Matcher m = p.matcher(text);

        if (m.find()) {
            return Integer.valueOf(m.group(1));
        }
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

不要注意方法的名称,它们都是可选的.

  • 我想单独测试sendRequest(String json, String text)方法.
  • 我想存根方法isMethod()getMessageId(json),并验证 …

java mockito

6
推荐指数
2
解决办法
2万
查看次数

Java单元测试:如何验证是否调用了私有方法?

假设我们有以下代码块:

public void foo(){
    bar();
}

private void bar(){
    //do something in here
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在jUnit测试中验证是否调用了bar()方法?使用Mockito或其他框架是否可以做到这一点?

java unit-testing mockito

5
推荐指数
0
解决办法
2850
查看次数

单元测试用于调用getter和setter的映射方法

假设我想为这样的方法创建一个单元测试:

public Car map(CarReq request) {

    Car car = new Car();
    car.setPrice(carReq.getPrice());
    car.setColour(carReq.getColour());
    car.setType(carReq.getType());

    // Other 20 lines like these

    return car;
}
Run Code Online (Sandbox Code Playgroud)

我可以模拟carRequest并告诉每个方法应该返回什么。但这就像没有测试任何东西一样,因为所有方法所做的都是从carReq获取值。

我可以创建一个测试carReq对象(不带模拟),并检查是否将相同的值复制到输出Car对象中。但这是很多工作,对吗?

有没有更聪明的方法?

java unit-testing

2
推荐指数
1
解决办法
1872
查看次数

在单元测试中模拟/存根 RuntimeException

School课堂上,我有一个start()调用另一个函数的函数doTask()

pubic class School {
    public void start() {
       try {
         doTask();
       } catch(RuntimeException e) {
          handleException();
       }
    }
    private void doTask() {
      //Code which might throw RuntimeException
    }
}
Run Code Online (Sandbox Code Playgroud)

我想单元测试start()RuntimeException

@Test
public void testStartWithException() {
  // How can I mock/stub mySchool.start() to throw RuntimeException?
  mySchool.start();
}
Run Code Online (Sandbox Code Playgroud)

我的实现代码抛出 并不容易RuntimeException,如何让测试代码模拟 RuntimeException 并抛出它?

(除了纯 JUnit,我正在考虑使用Mockito,但不确定如何抛出 RuntimeException)

java junit unit-testing mockito

2
推荐指数
1
解决办法
7947
查看次数

标签 统计

java ×8

mockito ×5

unit-testing ×4

junit ×3

mocking ×3

powermock ×2

jmockit ×1