Java – is this integration testing or unit testing?

It's not just a real case, it's the problem I encountered when trying to get the detailed difference between unit test and integration test

Suppose I have a class sum, which adds two integers:

class Sum{
  int x;
  int y;
  public int add(){
    return x + y;
  }
  ...getters and setters...
}

I also have another class responsible for validating the results to confirm that the values are expected For example only, suppose we just want to add positive numbers:

class ValidateSum{
   Sum sum;
   public boolean validate(){
      if(sum.getX()>=0 and sum.getY()>=0){
          return true;
      }
      else{
          return false;
      }
   }
   ... getters and setters...
}

Having validatesum may not make much sense, but we just assume it for example

Now I want to write tests for validatesum If I do this:

@Test
public void testValidateSum(){
    ValidateSum vs = new ValidateSum();
    Sum sum = new Sum();
    vs.setSum(sum);
    boolean result = vs.validate();

    assertTrue(result);
}

Is this unit test or integration test?

I know that the unit test only needs to verify the functions in validatesum, and to some extent, the test does this: it only gets properties from sum, not any of its functions

On the other hand, you can also say that you are accessing functions from sum, even if validatesum only calls getter Any change in the getter of sum will affect the testing of validatesum, breaking the concept of unit testing

But if this is the case and it is indeed an integration test, how can I write a unit test for validate () of validatemethod?

The only thing I didn't expect was to laugh at sum, so it returned the same value Even if the logic in sum's getter changes, validatesum's test will remain unchanged The problem is that the simulated response of getters may increase unnecessary complexity, because the possibility of logical changes in getters is very low, and all we do is get properties

I hope my question is reasonable, which is more a theoretical question

edit

Thank you for your answer They have important things to consider One of the best answers I chose was because it led me to:

http://www.mockobjects.com/2007/04/test-smell-everything-is-mocked.html

It's true: in theory, I have to simulate sum to become a pure unit test However, for most cases, the cost of increasing complexity and the energy spent by simulation attribute acquirers are not worth the "purest" unit test, but in practice, the difference between unit and integration test is subjective

Solution

Don't laugh at sum Generally speaking, getters and makers have no logic Simulation is the process of deleting class logic, so it does not interfere with the testing of another class It is meaningless to simulate a class without any logic, or a class whose logic is irrelevant to the current test In fact, "don't simulate value objects" is a well - known unit testing principle

Your test does not use any logic of the sum class So this is a unit test

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>