模拟中的依赖太多:单元测试问题

发布于 2024-10-14 12:33:41 字数 162 浏览 1 评论 0原文

我想要对 Business 类进行测试,但遇到了这个问题:其中一个模拟对象对其他类(例如 Sites、URL 和 ComplexObject)有许多依赖项。

我的问题是:如果我必须在我需要测试的方法中使用我的模拟对象的这个方法,我该如何隔离我的类?我应该模拟所有这些并将它们添加到模拟对象中吗?

I want to do a test of a Business class and I have this problem: one of the mocking objects has many dependencies to other classes such as Sites, URL, and ComplexObject.

My question is: how can I isolate my class if I have to use this method of my mock object in the method i need to test? Should I mock all of them and add them to the mocked object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-10-21 12:33:41

您还能如何测试它?看起来您需要模拟依赖项。一个积极的方面是您可以在其他需要测试的类中使用模拟。请注意,这是一个明确的代码气味

您是否考虑过依赖注入?如果您传入了所有依赖项,则可以创建一个工厂来生成一组测试依赖项,然后仅覆盖测试所需的依赖项。

How else can you test it? It looks like you'll need to mock the dependencies. One positive aspect is that you can likely use the mocks in other classes that need testing. Note that this is a definite code smell.

Have you thought about dependency injection? If you passed in all the dependencies you could create a factory that generates a set of testing dependencies and then override just the dependencies you need for your test.

灰色世界里的红玫瑰 2024-10-21 12:33:41

我喜欢做的是创建一个类,该类具有所有外部依赖项和静态方法的字段。例如:

public class DanceMakerDependecies{
    private URL url;

    public String getCurrentUser(){ // not static, so very easy to test
        return UserManager.currentUser().getName();
    }

    //getter and setters
}

public class DanceMaker{
     public DanceMaker(DanceMakerDependecies dep){
     ..
     }
     // you could also create default constructor with the default dependencies
}

public class DanceMakerTest{
    @Test
    void dance(){
        DanceMaker dm = new DanceMaker();
        dm.setDependecies(EasyMock.createMock(DanceMakerDependecies.class));
        //etc.
    }
}

我知道纯粹主义者更喜欢将所有内容注入到类中,但我发现这种测试方式要简单得多。尝试一下,看看你对它的看法(我敢打赌它不是最佳实践或设计模式,但我喜欢它)。

What I like to do is to create a class that has field to all outside dependecies and also static methods. for example:

public class DanceMakerDependecies{
    private URL url;

    public String getCurrentUser(){ // not static, so very easy to test
        return UserManager.currentUser().getName();
    }

    //getter and setters
}

public class DanceMaker{
     public DanceMaker(DanceMakerDependecies dep){
     ..
     }
     // you could also create default constructor with the default dependencies
}

public class DanceMakerTest{
    @Test
    void dance(){
        DanceMaker dm = new DanceMaker();
        dm.setDependecies(EasyMock.createMock(DanceMakerDependecies.class));
        //etc.
    }
}

I know that purists will prefer to just to inject everything into the class, but I find this way of testing much more simple. try it and see what you think about it(i bet its not best-pratice or design pattern, but i like it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文