在类变量上间谍
我有一个奇怪的场景,我在其中创建一个空列表作为类变量,然后我有一个调度程序来更新该列表。在我的测试中,我希望能够查看该列表是否已更新。但是从我的理解中,这是不可能的。
public class Scheduler {
private List<String> list = new ArrayList<>(); //want to track this variable in my test
private final ListAccessor listAccessor;
Scheduler(final ListAccessor listAccessor) {
this.listAccessor = listAccessor
scheduler.scheduleAtFixedRate(this::updateString, 1, TimeUnit.HOURS);
}
public void updateString() {
try {
list = listAccessor.getUpdatedList(someParam); //want to see the value in my test after it gets updated
log.info("Successfully updated list");
} catch (final Exception e) {
log.error("Failed to get updated list", e);
}
}
}
@Test
public void testCheckForUpdate_listAccessorThrewError() {
Mockito.when(listAccessor.getUpdatedList(someParam)).thenThrow(Exception.class);
// assert that the list in my class wasn't updated
}
这样的事情吗?当列表未能更新时,我不想提出任何错误,因此似乎唯一的方法是以某种方式在列表中监视。任何帮助都赞赏
I have a weird scenario where I am creating an empty List as a class variable, then I have a scheduler that updates that list. In my tests, I want to be able to see whether or not that List was updated. But from my understanding this isn't possible.
public class Scheduler {
private List<String> list = new ArrayList<>(); //want to track this variable in my test
private final ListAccessor listAccessor;
Scheduler(final ListAccessor listAccessor) {
this.listAccessor = listAccessor
scheduler.scheduleAtFixedRate(this::updateString, 1, TimeUnit.HOURS);
}
public void updateString() {
try {
list = listAccessor.getUpdatedList(someParam); //want to see the value in my test after it gets updated
log.info("Successfully updated list");
} catch (final Exception e) {
log.error("Failed to get updated list", e);
}
}
}
@Test
public void testCheckForUpdate_listAccessorThrewError() {
Mockito.when(listAccessor.getUpdatedList(someParam)).thenThrow(Exception.class);
// assert that the list in my class wasn't updated
}
Is something like this possible? I don't want to throw any errors when the list fails to update, so it seems the only way is to spy on the list somehow. Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论