Aaron
Aaron

Reputation: 31

JUnit testing issue

I'm trying to do a JUnit test to a class in a different package.

The test keeps giving me the error 'Target cannot be resolved to a type'

Here is my JUnit test code

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);
    }

}

and here under is the class which I'm getting the method from

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) ;
}
}

Upvotes: 1

Views: 77

Answers (3)

janos
janos

Reputation: 124804

TargetTest is in the JUnit_Test package, the package of Target is unclear, and TargetTest doesn't import Target.

You either need to import Target in TargetTest, or move both in the same package.

Perhaps you just forgot to add this line at the top of Target.java:

package JUnit_Test;

Upvotes: 2

Sumit Kumar
Sumit Kumar

Reputation: 395

You are trying to test your class in different package. Put your class in one specified package.

Change your constructor to public since currently its having default scope and its has to be imported in JUnit_Test package.

Clean and build your project. It will work.

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

Upvotes: 0

duffymo
duffymo

Reputation: 309008

The problem is that the Target class is not in any package.

Put Target in a package and import it into your unit test.

All Java classes belong on a package, even if the default package is allowed by the compiler.

I'll offer some recommendations:

  1. Learn and follow the Java coding standards.
  2. Put the test and the class in the same package. You won't have this problem.
  3. Structure your projects following the Maven directory standard.

Upvotes: 0

Related Questions