Why can test private methods/fields in Spock without problems?

后端 未结 2 1505
Happy的楠姐
Happy的楠姐 2021-02-10 06:28
package com.example.dev;
public class AClass {
 private Integer a =10;
...//other code
}

and when I try to access a in my Spock method:



        
2条回答
  •  孤街浪徒
    2021-02-10 06:56

    It's my understanding that

    aClassVar.a = new Integer(100)
    

    in Groovy / Spock is just syntactic sugar for

    aClassVar.setA(new Integer(100));
    

    in Java. There have been occasions where I tried to do that and there WASN'T a setter and Spock griped.

    As for why we create private attributes and then give them setters, THAT is another discussion.

提交回复
热议问题