- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class MainClass {
public void method1() {
…….
String str = getMethod2();
method3(str);
}
protected String getMethod2() {
String str = “Actual Value”
return str;
}
private void method3(String strparam) {
……
}
}
@RunWith(MockitoJunitRunner.class)
public class Testclass {
@InjectMocks
MainClass obj = new MainClass();
……
@Test
public void testSpy() {
MainClass spyMain = spy(obj);
when(spyMain.getMethod2()).thenReturn(new String("Testing spy"));
obj.method1();
……..
}
}
想要使用测试中 getMethod2() 返回的新值,因为它将传递给 method3。我的印象是,从测试类对 method1 的调用调用了 method2,并且我认为从 method2 返回的值是“测试 spy ”,但实际上是“实际值”,并且在运行测试类时“实际值”被传递给 method3。如何使用测试类中的新值?
最佳答案
您正在 obj
(obj.method1();
) 上调用该方法,而不是在您监视的对象上调用该方法:spyMain.method1 ();
.
关于java - Spy对象值不随when改变,thenReturn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887790/
public class MainClass { public void method1() { ……. String str = getMethod2();
我试图从when().theReturn 返回一个迭代器,但我不断收到此错误: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Itr
我正在尝试为我的 Spring Controller 编写测试,但遇到了问题。以下代码始终返回 redirect:/welcome 尽管我有 when(result.hasErrors()).then
这确实是一个新手问题,但我不知道如何解决这个问题。 我必须模拟一个方法来返回这样的类。 public Class getAClass(); 如果我做这样的事情 when(this.someInstan
经过大量研究,我没有在 Java 中的 JUnits 中找到这个问题的答案。 我想要做的是:对when().thenReturn(object)调用返回的对象调用一些方法。 例如: public b
我在测试中使用 PowerMockito 模拟静态缓存。一般来说,缓存的工作原理是这样的: Cache.getInstance().findEntityById(AbstractDTO); // so
我能够调整我的测试,但它失败了。问题是,模拟方法仍然返回错误数据。这是我要测试的方法: fun getTextByLanguage(list: List) : String { val dev
我有一个接受项目列表的方法。我希望模拟方法返回相同大小的相应列表(即 List ) 更一般地说,是否可以根据给定的输入动态设置返回值? 问题是我对同一个方法进行了多次测试。测试 A 用于空列表,测试
我已经阅读了此处的所有其他主题,但找不到正确的解决方案。 我正在测试一个调用我想模拟的服务的 Controller 。如果在测试中我控制了 when().thenReturn() 规则的结果,那么它就
我是 Mockito 的新手,我对 thenReturn 方法有疑问。我已经阅读了此类解决方案运行良好的教程,但在我的程序中,与上述示例相比,肯定存在任何不一致之处。 @RunWith(Mockito
我有一个具有 2 个函数的 A 类:函数 a() 返回一个随机数。调用 a() 并返回返回值的函数 b()。 在测试中我写了这个: A test = Mockito.mock(A.class) Moc
我有一个 Tuple 模拟类,它的 getString(0) 和 getString(1) 方法预计会被调用 n 次。而不是写类似的东西, when(tuple.getString(0)).thenR
我正在尝试实现 Mockito 来测试一个特定的方法,但是 .thenReturn(...) 似乎总是返回一个空对象而不是我想要的: 剪切: public class TestClassFacade
我正在研究继承的代码。我编写了一个应该捕获 NullPointerException 的测试(因为它试图从 null 对象调用方法) @Test(expected=NullPointerExcepti
我在 Mockito 中有这个: when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn(new Servi
考虑以下类(使用 CDI + 剪切 Restclient) public class A { @Inject Restclient client; public Object init(Stri
我模拟了一个名为 methodA() 的方法。 我有一个名为 linkedListA 的链表。现在, 我有一行代码来模拟 methodA 的返回,这样 when(methodA()).thenRetu
我一直在试图弄清楚为什么当我有 when(controller.findIngredientsByCategory(any()).thenReturn(Collections.emptyList())
运行以下代码时,我收到错误消息 Unfinished Stubbing here detected: import static org.mockito.Mockito.mock; import st
测试类 public class CollectionImplementationUnitTest { CollectionImplementation colImp; public void
我是一名优秀的程序员,十分优秀!