Under what conditions should get () and set () methods be tested?

I could not confirm whether to do these tests. It seems the set and get method is as simple as:

setA(String A) {
    this.A = A;
} 

getA(){
    return A;
}

Any ideas would be appreciated!

Thank you Joseph

+3
source share
9 answers

General rule: Not a lot of writing tests for getters and setters. Only if they have additional logic, i.e. are not pure accessories, you should write tests.

+4
source

I saw very few problems with getters and setters in the wild, and only one of them could be detected through unit test, and only if all getters and setters were tested together, rather than using individual testing methods.

/ /. .

public void setA(Object a) {
  this.a = a;
}
public Object getA() {
  return a;
}

public void setB(Object a) {
  this.a = a;
}
public Object getB() {
  return a;
}

, / , .

IDE , , IDE. ( vi .) - , .

, ( ), , , .

, , , , , "" , , - , , - , , .

+8

, set() get(), - . . 1 8


public void SetA(int a)
{
  if(a > 8 || a < 1)
  {
    throw new IndexOutOfBoundsException();
  }

  this.a = a;
}

, , , . , - , 9 1:)

+2

/

  • , /

  • ,

  • , testuite


, , , .

+1

, , , , - , ; -)

. , / .

, - setter/getter - , , .

+1

: ", ". , , , . , " ", . , , , , .

+1

, -. (, , 2 ), ( POSIX, , , )

0

, , . A -, .

EDIT: , , "overdrawn", , remove().

0

, . , , , - , . , , , - , get/set, , .

0

Source: https://habr.com/ru/post/1747299/


All Articles