Jeanne Boyarsky wrote:Glenn,
It is a matter of style. I don't have any problems with a test having multiple assertions that cover the same scenario. Some people believe a test should only have one assertion.
I do have two observations:
1) It appears you are testing the value passed into the constructor is the value returned by getName(). If so, you could create a String variable so you aren't duplicating that String and to make the intent clearer.
2) Does the getName() method have logic in it? If it just returns the value (vs doing conversion/validation), most of your tests are overkill.
Ya it's a getter method.
I know now i shouldn't be testing that cause it's a waste of time.
I did have another question though, made test for something that had logic:
I was reading somewhere that unit tests should be self contained.
Is it ok to declare private variables at the top like this.
Or within each method must I repeat instantiating the object in each one.
This way it's less repetitive but I worry that the object could possibly change between test methods and if it's wrong in one it could affect the other.
If that's the case i might just have to instantiate in each method.