2016-07-31 222 views
1

我正在尝试对另一个包中的类执行JUnit测试。JUnit测试问题

测试一直给我的错误“目标无法解析为一个类型”

这里是我的JUnit测试代码

package JUnit_Test; 

import static org.junit.Assert.*; 

public class TargetTest { 

    public void test() { 

     Target t = new Target (200,200,50,3,1,0.2,100,100,true); 

     t.move(); 
     assertEquals(t.positionX, 11); 
     assertEquals(t.positionY, 15); 
    } 

} 

,并在此之下是我得到的类从

import java.awt.Color; 
import java.util.Random; 

public class Target { 
    private Boolean animationdie=false; 
    private int size;//size of target 
    private int movementschem;//movement scheme target 
    private int subScenario; 
    private int posx;//were is targets x 
    private int posy;//were is targets y 
    private Boolean isclickable;//can we click 
    //for drawing animation circle 
    private int angle=0;//angle position of targets 
    private int angler=0;//radius angle 
    private Double radius;// radius of roads target 
    private int centerx;//position of centre target 
    private int centery;//position of centre target 
    private Color MyColor;//Colour targets 
    private Random rand;//for Random 
    //Create simple target 
    Target(int x,int y,int s,int movement,int scenario,Double rad,int centx,int centy, boolean isclickable){ 
     rand = new Random(); 
     size=s; 
     posx=x; 
     posy=y; 
     movementschem=movement; 
     subScenario=scenario; 
     radius=rad; 
     centerx=centx; 
     centery=centy; 
     MyColor= new Color(rand.nextInt(236)+20,rand.nextInt(236)+20,rand.nextInt(236)+20); 
     animationdie=false; 
     this.isclickable=isclickable; 
    } 

    //redraw target in random position 
    public void allrand(){ 
     rand = new Random(); 
    size=rand.nextInt(40)+20; 
    posx=rand.nextInt(400); 
    posy=rand.nextInt(400); 
    movementschem=rand.nextInt(5); 
    subScenario=rand.nextInt(4); 
    radius=rand.nextDouble()*rand.nextInt(100); 
    centerx=rand.nextInt(200)+100; 
    centery=rand.nextInt(200)+100; 
    MyColor= new Color(rand.nextInt(236)+20,rand.nextInt(236)+20,rand.nextInt(236)+20); 
    isclickable=true; 
    animationdie=false; 
} 




public void move() { 
    switch (getmovementschem()) { 
    case 0: { // From one side to the other, in a single 
       // axis 
     switch (getsubScenario()) { 
     case 0: // From left to right 
      posx+=1; 
      if (posx > 500) { 
       posx = -30; 
      } 
      break; 
     case 1: // From right to left. 
      setx(getx() - 1); 
      if (getx() < 0) { 
       setx(500); 
      } 
      break; 
     case 2: // From top to bottom. 
      sety(gety() + 1); 
      if (gety() > 500) { 
       sety(-50); 
      } 
      break; 
     case 3: // From bottom to top. 
      sety(gety() - 1); 
      if (gety() < 0) { 
       sety(500); 
      } 
      break; 
     } 
     break; 
    } 
    case 1: {// From one side to the other, in a two 
       // axis 
     switch (getsubScenario()) { 

     case 0: // From left to right 
      setx(getx() + 1); 
      sety(gety() + 1); 
      break; 
     case 1: // From right to left. 
      setx(getx() - 1); 
      sety(gety() - 1); 
      break; 
     case 2: // From top to bottom. 
      sety(gety() + 1); 
      setx(getx() - 1); 
      break; 
     case 3: // From bottom to top. 
      sety(gety() - 1); 
      setx(getx() + 1); 
      break; 
     } 
     break; 
    } 
    case 2: { // Halfway to the other side of the 
       // screen, and then back again 

     switch (getsubScenario()) { 

     case 0: // From left to right 
      setx(getx() + 1); 
      if (getx() > 250) { 
       setsubScenario(1); 
      } 
      break; 
     case 1: // From right to left. 
      setx(getx() - 1); 
      if (getx() < 0) { 
       setsubScenario(0); 
      } 
      break; 
     case 2: // From top to bottom. 
      sety(gety() + 1); 
      if (gety() > 250) { 
       setsubScenario(3); 
      } 
      break; 
     case 3: // From bottom to top. 
      sety(gety() - 1); 
      if (gety() < 0) { 
       setsubScenario(2); 
      } 
      break; 
     } 
     break; 
    } 

    case 3: { // In a circle, starting at the random 
       // part of the screen and moving 
       // outwards towards 

     if (getangle() > 360) { 
      setangle(0); 
     } else { 
      setangle(getangle() + 1); 
     } 
     double degrees = getangle()/(180/Math.PI); 

     int X = (int) (getcenterx() + (getradius()) * Math.cos(degrees)); 
     int Y = (int) (getcentery() + (getradius()) * Math.sin(degrees)); 

     sety(Y); 
     setx(X); 

     break; 
    } 

    case 4: { // In spirals from one side of the screen 
       // to the other 

     if (getangle() > 360) { 
      setangle(0); 
     } else { 
      setangle(getangle() + 1); 
     } 
     double degrees = getangle()/(180/Math.PI); 
     if (getradius() > 450) { 
      setradius(rand.nextDouble() * rand.nextInt(100)); 
     } 
     setradius(getradius() + 0.1); 
     int X = (int) (getcenterx() + (getradius()) * Math.cos(degrees)); 
     int Y = (int) (getcentery() + (getradius()) * Math.sin(degrees)); 

     sety(Y); 
     setx(X); 

     break; 
    } 

    case 5: { // In an arc from one side of the screen 
       // to the other 

     if (getangle() > 360) { 
      setangle(0); 
     } else { 
      setangle(getangle() + 1); 
     } 
     double degrees = getangle()/(180/Math.PI); 

     setradius(150.0); 

     if (getradius() > 450) { 
      setradius(rand.nextDouble() * rand.nextInt(100)); 
     } 
     int X = (int) (getcenterx() + (getradius() + 150) * Math.cos(degrees)); 
     int Y = (int) (getcentery() + (getradius() + 20) * Math.sin(degrees)); 

     sety(Y); 
     setx(X); 

     break; 
    } 
    } 
} 



/* 
* for work with class 
* */ 
public Boolean getisclicable(){ 
    return isclickable; 
} 
public Color getcolor(){ 
    return MyColor; 
} 
public Double getradius(){ 
    return radius; 
} 
public int getcenterx(){ 
    return centerx; 
} 
public int getcentery(){ 
    return centery; 
} 


public int getmovementschem(){ 
    return movementschem; 
} 
public int getsubScenario(){ 
    return subScenario; 
} 
public int getangle(){ 
    return angle; 
} 
public int getangler(){ 
    return angler; 
} 
public int getx(){ 
    return posx; 
} 
public int gety(){ 
    return posy; 
} 
public int getsize(){ 
    return size; 
} 
public void setsubScenario(int scenario){ 
    subScenario=scenario; 
} 

public void setsize(int s){ 
    size=s; 
} 
public void setx(int x){ 
    posx=x; 
} 
public void sety(int y){ 
    posy=y; 
} 

public void setangle(int an){ 
    angle=an; 
} 


public void setangler(int an){ 
    angler=an; 
} 
public void setradius(Double rad){ 
    radius=rad; 
} 
public void setcenterx(int rx){ 
    centerx=rx; 
} 
public void setcentery(int ry){ 
    centery=ry; 
} 
public Boolean getAnimationdie() { 
    return animationdie; 
} 
public void setAnimationdie(Boolean animationdie) { 
    this.animationdie = animationdie; 
} 
public void setcolor(int r,int g,int b){ 
    MyColor= new Color(r,g,b) ; 
} 
} 

回答

2

TargetTest的方法是在JUnit_Test包,的包不清楚,TargetTest不导入Target

您或者需要导入TargetTargetTest,或者在同一个包中移动两者。

也许你只是忘了在Target.java顶部添加这一行:

package JUnit_Test; 
0

的问题是Target类不在任何包。

Target放入包中,并将其导入到您的单元测试中。

即使编译器允许使用默认包,所有Java类都属于包。

我会提供一些建议:

  1. 学习和遵循Java编码标准。
  2. 把测试和班级放在同一个包里。你不会有这个问题。
  3. 按照Maven目录标准构建您的项目。
0

您正试图在不同的包中测试您的课程。 把你的课堂放在一个指定的包中。

将您的构造函数更改为public,因为它目前具有默认范围,并且必须将其导入到JUnit_Test包中。

清理并建立您的项目。它会工作。

public Target(int x,int y,int s,int movement,int scenario,Double rad,int centx,int centy, boolean isclickable)