This sounds more vague than I mean, but when testing a method in a class what is the correct process.
e.g. a customer class saves a set password’s md5 hash to the db, not the actual password. A private method in the customer class works out the md5 hash for saving.
public class Customer() {
public void setPassword(String password){
this.password = hashPassword(password);
}
private String hashPassword(String password){
..do stuff..
}
.. other methods ..
}
Now this is just an example, I don’t want to know about how to calculate the md5 hash etc. It’s about the testing. Here’s my options I can think of:
- I create a method in the test class that does the same as hashPassword and then compare the results.
- I manually calculate what the outcome would be for a specific password (myPassword) and store as a constant. After setting the password to myPassword in the test I would compare the constant to the result.
You should take the second option, i.e. pre-calculate a value or a sequence of values, and test with them. Testing a method with a copy of the same method serves no purpose other than increasing the amount of code duplication.