简体   繁体   中英

Make my parallel test work parameterized

I'm skill new with junit testing system and I would like to get help from any one who is capable of helping to fix the @Parameters. In the array suppose to have three values but I am not sure what to put in it. I simply need help in object test conditions, by making the parallelTo test work. I am open to any other suggestion. Thanks in advance

LineTestParallelTo.java

package line;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class LineTestParallelTo {


private int parallel;
private Line p;
private boolean x;

public void LineTestParallel(int parallel, Line p)
{
    this.parallel = parallel;
    this.p = p; 
    this.x = x;
}

@Parameters
public static Collection<Object[]> testConditions() {

    Line one = new Line(1,1,2,2);
    Line two = new Line(1,1,3,3);
    Line three = new Line(1,1,3,2);

    Object x[][][] = {
            {},
            {},
            {}
    };
    return Arrays.asList(x);
}

@Test
public void test() {
    assertEquals(parallel, x.parallelTo(l) , p.parallelTo(p), 0.0001);
}

}

Line.java

package line;

public class Line {
//Always create a constructor
//private double x0, y0, x1, y1;

// construct a line object
  public Line(double x0, double y0, double x1, double y1) {
    this.x0 = x0;
    this.y0 = y0;
    this.x1 = x1;
    this.y1 = y1;
  }



  // calculate the slope of the line
  public double getSlope( ) {
    // avoid dividing by zero
    if(x1 == x0) {
      throw new ArithmeticException( );
    }

    return (y1 - y0) / (x1 - x0);
  }

  // calculate the distance of the line
  public double getDistance( ) {
    return Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
  }

  // return whether a line is parallel to another
  public boolean parallelTo(Line l) {
    // if the difference between the slopes is very small, consider them parallel
    if(Math.abs(getSlope( ) - l.getSlope( )) < .0001) {
      return true;
    } else {
      return false;
    }
  }

  // private member data
  private double x0, y0, x1, y1;


}

The values you need are the values for the 3 parameters you defined as fields:

private int parallel;
private Line p;
private boolean x;

As an example:

return Arrays.asList(new Object[][][]{
            {1, one, false},
            {2, two, true},
            {3, three, true}
    });

For those interested in how I got to this: Junit Parameters Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM