Java 单元测试三角形
我有一个接口Java 单元测试三角形,java,unit-testing,junit,Java,Unit Testing,Junit,我有一个接口 public interface Triangle { int X1(); int Y1(); int X2(); int Y2(); int X3(); int Y3();} 它包含返回6个整数的方法,这些整数是直角三角形三个顶点在笛卡尔坐标系中的坐标。 我有一个方法,返回一个直角三角形: public final class RtriangleProvider { public static Rtriangle getRtriangle() { } } 我应该编写代码one
public interface Triangle {
int X1();
int Y1();
int X2();
int Y2();
int X3();
int Y3();}
它包含返回6个整数的方法,这些整数是直角三角形三个顶点在笛卡尔坐标系中的坐标。
我有一个方法,返回一个直角三角形:
public final class RtriangleProvider {
public static Rtriangle getRtriangle() {
}
}
我应该编写代码one junit测试方法getRtriangle,它确实返回一个直角
三角形。
以下是我的单元测试:
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class RtriangleProviderTest{
@Test
public void Test(){
Rtriangle test = RtriangleProvider.getRtriangle();
assertNotNull("The input data are not available", test);
double a = Math.sqrt(Math.pow((test.getX1()-test.getX2()),2)+(int)Math.pow((test.getY1()-test.getY2()),2));
double b = Math.sqrt(Math.pow((test.getX2()-test.getX3()),2)+(int)Math.pow((test.getY2()-test.getY3()),2));
double c = Math.sqrt(Math.pow((test.getX1()-test.getX3()),2)+(int)Math.pow((test.getY1()-test.getY3()),2));
assertTrue ("The sum of two sides is less than the third, not the triangle",a < b + c && b < c + a && c < a + b);
assertTrue("Not rectangle triangle in accordance with Pythagorian theorem",((int)Math.pow(a,2)==(int)Math.pow(b,2)+(int)Math.pow(c,2))||
((int)Math.pow(b,2) == (int)Math.pow(a,2)+(int)Math.pow(c,2))||
((int)Math.pow(c,2) == (int)Math.pow(b,2)+(int)Math.pow(b,2))
);
}
}
import org.junit.Test;
导入静态org.junit.Assert.assertNotNull;
导入静态org.junit.Assert.assertTrue;
公共类RtriangleProviderTest{
@试验
公开无效测试(){
Rtriangle test=RtriangleProvider.getRtriangle();
assertNotNull(“输入数据不可用”,测试);
double a=Math.sqrt(Math.pow((test.getX1()-test.getX2()),2)+(int)Math.pow((test.getY1()-test.getY2()),2));
double b=Math.sqrt(Math.pow((test.getX2()-test.getX3()),2)+(int)Math.pow((test.getY2()-test.getY3()),2));
double c=Math.sqrt(Math.pow((test.getX1()-test.getX3()),2)+(int)Math.pow((test.getY1()-test.getY3()),2));
assertTrue(“两条边的总和小于第三条,而不是三角形”,a
我想,我的测试还没有完成。请告诉我我的测试出了什么问题好吧,这可能不是你想听到的,但我认为所有的计算都必须放在三角形对象中。所以你会得到这样的结果:
public interface Triangle {
int X1();
int Y1();
int X2();
int Y2();
int X3();
int Y3();
double a();
double b();
double c();
boolean isTheSumOfSidesLessThanAThird();
boolean isATriangleInAccordancePythagorianTheorem();
}
重构triangle类后,我将整理断言:
// Given...
Rtriangle test = RtriangleProvider.getRtriangle();
// When and Then...
assertThat(test, isNotNullValue());
assertThat(test.isTheSumOfSidesLessThanAThird(), isTrue());
assertThat(test.isATriangleInAccordanceWithPythagorianTheorem(), isTrue());
我认为,判断边的和是否小于3,如果三角形是毕达哥拉斯三角形,那么calc属于三角形本身,因此我首先将这些方法移动到triangle类
您应该避免在测试本身中保留复杂的逻辑(例如这些计算),因为您在编写测试代码时可能会出错,并且,根据您所犯的错误,尽管测试断言了错误的值,但它将以优异的成绩通过测试。有些情况下,您无法避免在测试中编写复杂的逻辑,但必须尽一切努力避免陷入这些情况
我不知道这个RTriangleProvider类是如何工作的,但是您应该能够模拟它的行为,以便它为Xs和Ys设置已知的值,因此您应该知道在调用a()、b()、c()、IsTheSumofSideslSSThanathird()和IsaTriangleInCordancewithPythagoriantheOrem()时会发生什么
请记住: