2016-07-31 277 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)