I’m learning Java by reading “Head First Java” and by doing all the puzzles and excercies. In the book they recommend to write TestDrive classes to test the code and clases I’ve written, that’s one really simple thing to do, but by doing this I think I can’t fully test my code because I’m writing the test code knowing what I want to get, I don’t know if it makes any sense, but I was wondering if there’s any way of testing my code in a simple way that it tell’s me what isn’t working correctly. Thanks.
Share
What do we mean by code? When Unit testing, which is what I think we’re talking about here, we are testing specific methods and classes.
In other words you are investigating whether some code fulfils a contract. Consider this example:
What tests can you devise? If you devise a good set of tests you can have some confidence in this routine. So we could try these kinds of input:
What else? How about a negative rate?
More interestingly, how about a very high rate of interest like 1,000,000.50 and 100,000 years, what happens to the result, would it fit in an integer – the thing about devising this test is that it challenges the interface – why is there no exception documented?
The question then comes: how do we figure out those test cases. I don’t think there is a single approach that leads to building a comprehensive set but here’s a couple of things to consider: