如何用void返回类型模拟方法?
我实现了一个观察者模式,但我无法用Mockito模拟它,因为我不知道如何.
我试图在互联网上找到一个例子,但没有成功.
我的班级看起来像
public class World {
List<Listener> listeners;
void addListener(Listener item) {
listeners.add(item);
}
void doAction(Action goal,Object obj) {
setState("i received");
goal.doAction(obj);
setState("i finished");
}
private string state;
//setter getter state
}
public class WorldTest implements Listener {
@Test public void word{
World w= mock(World.class);
w.addListener(this);
...
...
}
}
interface Listener {
void doAction();
}
Run Code Online (Sandbox Code Playgroud)
系统不会通过模拟触发.=(我想显示上面提到的系统状态.并根据它们做出断言.
当试图使用Spring的Mockito时,通过bean声明创建Mock对象......
<bean id="accountMapper" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.example.persistence.mybatis.mappers.AccountMapper" />
</bean>
Run Code Online (Sandbox Code Playgroud)
...我在调用Mockito时发现了一些奇怪的行为.多次没有重置Mock对象,例如:
Mockito.when(this.accountMapper.createBadGrammarException()).thenThrow(new BadSqlGrammarException("Bla", null, new SQLException()));
Run Code Online (Sandbox Code Playgroud)
一旦在测试期间(在同一模拟器上)多次调用此代码("Mockito.when"),测试就会失败并出现错误(BadSqlGrammerException,即使此异常是实际预期的 - 我确实失败了如果我不抛出异常,并手动抛出它工作正常).这是预期的行为吗?Mockito似乎建议每次创建一个新的模拟,这意味着为每个方法创建DAO ......?
当我两次调用Mockito.when方法时到底发生了什么?模拟应该如何反应?替换行为?忽略它?不幸的是,大多数搜索仅产生如何为方法本身的多次调用返回不同结果的结果,但不会产生多次调用Mockito所期望的结果.当...
我只是想在这里了解Mockito和最佳实践,因为只是因为它看起来很有效,所以选择一些东西似乎是一个坏主意......
我有一个非常复杂的方法,我想测试其行为(使用 Mockito 和 JUnit)。该方法将一个对象(我们称之为它的 type State)作为输入,并且应该考虑一些不同的状态变量来决定其输出。
作为示例,考虑以下规范(s是该类的模拟State):
s.varOne已设置,则返回其值。s.varTwo已设置,则返回该值。s.varThree已设置,则调用s.update(s.varThree)然后 return s.varOne,该值现在将具有一个值(即使在第 1 阶段没有)。为了正确测试案例 3,我想设置该s对象,以便s.varOne和s.varTwo都一开始就未设置,但如果(且仅当!) sut 调用s.update(s.varThree),然后s.varOne返回一些内容。
在 Mockito 中是否有设置此行为的好方法?
我考虑过为 建立一些返回值链s.varOne,然后验证调用的顺序是否与输出的顺序相对应(当然,sut 的返回值也是正确的),但这感觉很脏; 如果我随后更改方法以其他方式计算其返回值,即调用s.varOne不同次数但不更改其输出,那么即使功能相同,测试也会失败。
我理想的解决方案是一种可以为模拟对象添加一些“延迟”设置的方法,该设置在 sut 调用该s.update()方法时运行,但我无法找到实现该目标的方法。
我希望模拟对象在每个方法调用上返回不同的值。但是该方法没有参数。下面是一个例子:
public class MyClass {
public double getValue() {
return 0;
}
}
public class IteratorClass {
MyClass myClass;
public IteratorClass(MyClass myClass) {
this.myClass = myClass;
}
public void iterate() {
for (int i = 0; i < 5; i++) {
System.out.println("myClass.getValue() = " + myClass.getValue());
}
}
}
public class IteratorClassTest {
private MyClass myClass;
private IteratorClass iteratorClass;
@Before
public void setUp() throws Exception {
myClass = mock(MyClass.class);
iteratorClass = spy(new IteratorClass(myClass));
}
@Test
public void testIterate() throws Exception …Run Code Online (Sandbox Code Playgroud) 我正在模拟一个用于向远程Http服务提交一些对象的接口,逻辑如下:如果提交成功,则尝试提交对象5次,然后继续下一个,否则尝试直到达到5次,如果仍然失败,则丢弃失败。
interface EmployeeEndPoint {
Response submit(Employee employee);
}
class Response {
String status;
public Response(String status) {
this.status = status;
}
}
class SomeService {
private EmployeeEndPoint employeeEndPoint;
void submit(Employee employee) {
Response response = employeeEndPoint.submit(employee);
if(response.status=="ERROR"){
//put this employee in a queue and then retry 5 more time if the call succeeds then skip otherwise keep trying until the 5th.
}
}
}
@Mock
EmployeeEndPoint employeeEndPoint;
@Test
public void shouldStopTryingSubmittingEmployeeWhenResponseReturnsSuccessValue() {
//I want the first
Employee employee
= new …Run Code Online (Sandbox Code Playgroud)