here is the add method for polynomial
public Polynomilal add (Polynomial poly){
//getA()..etc getters for the coefficients of the polynomial.
MyDouble aVal=getA().add(poly.getA());
MyDouble b开发者_StackOverflowVal=getB().add(poly.getB());
MyDouble cVal=getC().add(poly.getC());
Polynomial addedPoly=new Polynomial(aVal, bVal, cVal);
return addedPoly;
}
and the test case for add method starts with
public void testAdd() {
........
........
}
Here's some of the basics...
The general idea of a unit test is to compare "What you want" with "What you got". A simple assertion is like
assertEquals("it better work!", 4 /* expected */, 2 + 2);
If you know what aVal should be, you can do
assertEquals("aVal should be this", <what you expecte it to be>, aVal);
There's a special detail for "double" values, because roundoff causes them to often be not exactly what you expect, so you say:
assertEquals("some double value", 1.555555d, 1.0d + 5.0d / 9.0d, .001); // within .001? ok!
Anyway, that's the gist of unit tests. Assertions of things you can see. Very handy stuff.
(assertEquals, and friends, are all statically accessible from TestCase, which most unit tests descend from.)
As addition to david's answer I recommend you to use Custom Assertion pattern described here. Also I'd consider using parametrized tests for input and expected data as described here and using some JUnit specific examples like the following.
Hope this helps!
精彩评论