Using TDD approach and avoiding Java static methods

血红的双手。 提交于 2019-12-23 12:38:55

问题


I just got some feedback about a job application Java coding exercise. They did not like the solution and two problems where stated in the feedback (which I'm very grateful for as it's very rare feedback is given):

  • I did not use TDD approach, apparently.
  • I overused static methods, I know static methods are anti OO but I only used them in validation and util type methods.

So two questions here:

What are the possible tell-tale signs of not using TDD approach?

What coding style or patterns can be an alternative to static methods?

Following the first two responses I have another question.

Do you agree that using static methods is only bad when it limits the testability of your code and not in them selves bad.

So going back to my job application exercise solution if the static methods do not limit the testability of my code is it still bad to use? my validate method was very simple 'Validator.notNull(p,"paramName")' now why would I ever want to mock that?

Many thanks.


回答1:


A tell-tale sign of not using TDD is usage of static methods and static class members for collaborators. You cannot override a static method, so you cannot substitute a mock to test the class using such methods in isolation.

Instead of using static collaborators or static methods on the collaborators, you can use dependency injection. In a simple coding exercise you would inject dependency via a constructor or via the setters by hand. In the real life you can use one of available dependency frameworks.




回答2:


Your static Validaton method seems something that should be part of an object to me.

Say you have an class Drink

public class Drink
{
    private readonly string _name;
    private readonly double _temperature;

    public Drink(string name, double temperature)
    {
        _name = name;
        _temperature = temperature;
    }
}

Your businesslogic would be able to instantiate all kinds of drinks, 7up, cola, whatever. You'd like to make sure that a drink has the appropriate temperature to drink it, so you have the need for a Validate method. You could follow your approach:

public void TakeAZip()
{
    if (Validation.HasAppropriateTemp)
    {
        // implement drink
    }
}

'

Alternatives for static classes

That way you have an hard dependency on your static Validation class.

Alternatively you could make use of dependency injection.

public void TakeAZip(ITemperatureValidator validator)
{
    if (validator.HasAppropriateTemp)
    {
        // implement drink
    }
}

If more convenient you could also choose to pass the Validator via the constructor

    private readonly string _name;
    private readonly double _temperature;
    private ITemperatureValidator _validator;

    public Drink(
        string name, 
        double temperature, 
        ITemperatureValidator validator)
    {
        _name = name;
        _temperature = temperature;
        _validator = validator;
    }

Now you can mock the behavior of your validator and you can isolate your Drink class from all external behavior.



来源:https://stackoverflow.com/questions/14986175/using-tdd-approach-and-avoiding-java-static-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!