Junit测试仍然失败,但代码是100%正确的

时间:2014-01-27 21:24:24

标签: java testing junit drjava

我是一名新的Java程序员,我一直遇到JUnit的问题 我在我的机器上运行了一个预先测试过的代码和测试,但是当它们传递给另一个时,它们都失败了。代码和测试是100%正常工作,我猜测我的电脑有问题(运行Windows 7和DrJava)。

我需要安装什么东西或者我必须做的其他事情吗?

此代码一直未通过测试,我无法弄清楚原因。如果你能告诉我这个错误,我会非常感激。

public class FeatureVector 
{
  private String name ;
  private double [] features ;
  private int size ; 
  public static boolean verbose = true ;



  public static boolean setVerbose ( boolean x  ) {verbose = x;
    return verbose ;} // static method to set the detailed message for printing



  public FeatureVector (String name , int size)
  { this.name = name;                             // the attributes -instanse variables-
    this.size = size ;
    this.features = new double [size] ;

    for (int x = 0 ; x < size ; x ++ )  {this.features[x] = 0.0 ;} // first constructor
  }

  public FeatureVector (String name , double [] elems) // second constructor 
  {
    this.name = name ;
    this.features = elems ;
    elems = null ; // set elems to null so we wont occupy un-needed  memory
    this.size = this.features.length ;

  }


  public String getName () { return this.name ;} // a name getter

  public int getSize ()  {return this.size ;}     //  a size getter 

  public double featureAt ( int index) { return this.features[index] ; } // an index getter


  public void featureSet (int index , double value ) { this.features[index] = value;} // an index setter


  public FeatureVector copy () 

  { FeatureVector copied = new FeatureVector ("a copy" , 0) ;

    copied.name = this.name ;

    for (int i  = 0 ; i < this.size ; i ++ ) {  copied.features[i] = this.features[i] ;}

    return copied ;}



  public double getDistance ( FeatureVector x )
  { double sum = 0 ;
    double values ;
    for (int m = 0 ; m < this.features.length ; m ++)
    {
      values = (this.features[m] - x.features[m])  ;
      Math.pow(values,2) ;
      sum = sum + values ;  }
    Math.sqrt(sum) ;
    return sum ;}

  public void plus (FeatureVector x ) 
  {
    for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i] + x.features[i] ;} }
  public void div (FeatureVector x) 
  { for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i] / x.features[i] ;}
  }
  public void div (double  x) 
  { for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i] / x ;}
  }


  public String toString () 
  { String message = ""  ; 

    if (details == true )
    { System.out.print (name + ": {" ) ; 
      for (int i = 0 ; i < this.features.length ; i ++)
      {System.out.print (this.features[i]) ;
        System.out.print (", ");}
      System.out.print ("}");
    }
    else { message = this.name ; }

    return message ; }

  public boolean equals (FeatureVector x )
  {
    boolean result ;
    if (this.features.length == x.features.length ) {result = true ;}
    else {result = false ; }
    return result ; }



}

并且测试

 import junit.framework.Assert; import junit.framework.TestCase; import
 junit.framework.TestSuite;

 public class FeatureVectorTest extends TestCase {

     public void testGetSize() {  FeatureVector v, w;

  v = new FeatureVector("V", 10);  Assert.assertEquals(10,
 v.getSize());

  w = new FeatureVector("W", 0);  Assert.assertEquals(0, w.getSize());
     }

     public void testFeatureAt() {  FeatureVector v = new FeatureVector("V", 10);

  for (int i = 0; i < 10; i++) {
      v.featureSet(i, (double) i);
      Assert.assertEquals((double) i, v.featureAt(i));  }
     }

     public void testFeatureVector() {  double[] vs = { -Math.PI, 1.6180339887, 2.7182818284, 1.4142135623 };  FeatureVector v = new FeatureVector("V", vs);

  for (int i = 0; i < vs.length; i++) {
      Assert.assertEquals(vs[i], v.featureAt(i));  }

  // What is the purpose of this test?  for (int i = 0; i < vs.length;
 i++) {
      double tmp = vs[i];
      vs[i] = 0.0;
      Assert.assertEquals(tmp, v.featureAt(i));  }
     }

     public void testGetDistance() {  FeatureVector v = new FeatureVector("V", new ouble[] { 0.0, 2.0, 1.0, 5.0 }); 
 FeatureVector w = new FeatureVector("W", new double[] { 2.0, 0.0,
 -1.0, 7.0 });

  Assert.assertEquals(0.0, v.getDistance(v), 0.00001); 
 Assert.assertEquals(0.0, w.getDistance(w), 0.00001);

  Assert.assertEquals(4.0, v.getDistance(w), 0.00001); 
 Assert.assertEquals(4.0, w.getDistance(v), 0.00001);

  v = new FeatureVector("V", 0);  w = new FeatureVector("W", 0);

  Assert.assertEquals(0.0, v.getDistance(w), 0.00001);
     }

     public void testEquals() {  FeatureVector v = new FeatureVector("V", new double[] { 0.0, 2.0, 1.0, 5.0 }); 
 FeatureVector w = new FeatureVector("W", new double[] { 2.0, 0.0, -1.0
 });  FeatureVector x = new FeatureVector("X", new double[] { 2.0, 0.0,
 -1.0 });  FeatureVector y = new FeatureVector("Y", new double[] { 0.0, 2.0, 1.0 });

  Assert.assertTrue(v.equals(v));  Assert.assertTrue(w.equals(w)); 
 Assert.assertTrue(w.equals(x));

  Assert.assertFalse(v.equals(w));  Assert.assertFalse(x.equals(y)); 
 Assert.assertFalse(x.equals(null));
     }

     public void testPlus() {  double[] vs = { -1.0, 0.0, 1.0, 2.0 };  double[] ws = { 1.0, 1.0, 1.0, 1.0 };  FeatureVector v = new
 FeatureVector("V", vs);  FeatureVector w = new FeatureVector("V", ws);

  v.plus(w);

  for (int i = 0; i < vs.length; i++) {
      Assert.assertEquals((double) i, v.featureAt(i));  }

     }

     public void testDiv() {  double[] vs = { 0.0, 2.0, 4.0, 6.0 };  FeatureVector v = new FeatureVector("V", vs);

  v.div(2.0);

  for (int i = 0; i < vs.length; i++) {
      Assert.assertEquals((double) i, v.featureAt(i));  }

     }

     public static void main( String[] args ) {  TestSuite suite = new TestSuite();  suite.addTestSuite( FeatureVectorTest.class ); 
 junit.textui.TestRunner.run( suite );
     }

 }

2 个答案:

答案 0 :(得分:1)

toString()方法中存在错误。您正在引用一个名为details的变量,该变量似乎不存在。您可能希望将其更改为verbose

答案 1 :(得分:0)

几年前,当我使用Eclipse之前,我已经让java莫名其妙地失败了。我正在运行一个简单的解析算法,我的代码拒绝承认1等于1.我使用了另一个IDE,它运行得很好。也许您遇到了一个奇怪的错误,另一个开发环境将运行您的代码。