是否可以模拟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) 我试图嘲笑模拟一个正在进行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) 在我使用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类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) 假设我们有一个名为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),并验证 …假设我们有以下代码块:
public void foo(){
bar();
}
private void bar(){
//do something in here
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在jUnit测试中验证是否调用了bar()方法?使用Mockito或其他框架是否可以做到这一点?
假设我想为这样的方法创建一个单元测试:
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对象中。但这是很多工作,对吗?
有没有更聪明的方法?
在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)