Java的原因是使用静态辅助方法不好? [英] Java why is using static helper methods bad?

查看:110
本文介绍了Java的原因是使用静态辅助方法不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这么问是因为我想用一个模拟框架(的Mockito),它不允许你嘲笑静态方法。寻找到它,我发现了不少的博客文章说,你应该有尽可能少的静态方法作为可能的,但我有困难环绕我的头周围的原因。特别是,为什么不修改全局状态,基本上都是辅助方法的方法。比如我有一类叫做 ApiCaller 有几个静态方法。其中一个静态方法的目的是为了执行HTTP调用,处理任何自定义的问题我们的服务器可能已经返回(例如没有登录的用户),并返回响应。为了简化,是这样的:

I'm asking because I'm trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I've found quite a few blog posts saying that you should have as few static methods as possible, but I'm having difficulty wrapping my head around why. Specifically why methods that don't modify the global state and are basically helper methods. For instance I have a class called ApiCaller that has several static methods. One of the static method's purpose is to execute an HTTP call, deal with any custom issues our server might have returned (ex. user not logged in) and return the response. To simplify, something like:

public class ApiCaller {
...
   public static String makeHttpCall(Url url) {
        // Performs logic to retrieve response and deal with custom server errors
        ...
        return response;
   }
}

要使用这一切我所要做的就是调用 ApiCaller.makeHttpCall(URL) 现在,我可以很容易地使之成为一个非静态方法,如:

To use this all I have to do is call ApiCaller.makeHttpCall(url) Now I could easily make this a non static method like:

public class ApiCaller {
...
   public String makeHttpCall(Url url) {
        // Performs logic to retrieve response and deal with custom server errors
        ...
        return response;
   }
}

,然后用这个方法调用新ApiCaller()。makeHttpCall()但这只是好像额外的开销。任何人都可以解释为什么这是不好的,如果有,以使这些方法非静态更好的解决方案(而不是只删除了关键字),这样我可以使用模拟框架存根出这些方法?

and then to use this method call new ApiCaller().makeHttpCall() but this just seems like extra overhead. Can anyone explain why this is bad and if there is a better solution to making the methods non static (other than just removing the keyword) so that I can stub out these methods using the mocking framework?

谢谢!

推荐答案

与静态方法的问题是他们很难伪造时,他们不与您正试图测试系统。想象一下code:

The problem with static methods is they're very hard to fake when they're not relevant to the system you're trying to test. Imagine this code:

public void systemUnderTest() {
    Log.connectToDatabaseForAuditing();
    doLogicYouWantToTest();
}

connectToDatabaseForAuditing()方法是静态的。你不关心这个方法会为你想写的考验。但是,你需要一个可用的数据库测试,现在这个code。

The connectToDatabaseForAuditing() method is static. You don't care what this method does for the test you want to write. But, to test this code now you need an available database.

如果它不是静态的code是这样的:

If it were not static the code would look like this:

private Logger log; //instantiate in a setter AKA dependency injection/inversion of control

public void systemUnderTest() {
    log.connectToDatabaseForAuditing();
    doLogicYouWantToTest();
}

和你的测试将是微不足道的,现在没有一个数据库中写:

And your test would be trivial to write without a database now:

@Before
public void setUp() {
    YourClass yourClass = new YourClass();
    yourClass.setLog(new NoOpLogger());

}

//.. your tests

试想一下这样做,当该方法是静态的。我真的不能想办法,除了修改记录器有一个叫做静态变量 inTestMode 您在设置设置为true() ,以确保它不会连接到数据库。

Imagine trying to do that when the method is static. I can't really think of a way except for modifying the logger to have a static variable called inTestMode that you set to true in the setUp() to make sure it doesn't connect to a database.

这篇关于Java的原因是使用静态辅助方法不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆