在类变量上间谍

发布于 2025-01-18 14:06:30 字数 1109 浏览 0 评论 0原文

我有一个奇怪的场景,我在其中创建一个空列表作为类变量,然后我有一个调度程序来更新该列表。在我的测试中,我希望能够查看该列表是否已更新。但是从我的理解中,这是不可能的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文