Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit测试_Java_Junit - Fatal编程技术网

Java JUnit测试

Java JUnit测试,java,junit,Java,Junit,我以前从未使用过JUnit测试。我需要用JUnit测试我的代码。 我已经在谷歌搜索了一整天,但我仍然不知道如何在我的代码中实现它。 例如,我用main方法测试了我的代码。我应该从代码中删除main方法吗? 另外,可能是由于我的错误,我找不到与char变量相关的assert方法。 有人能帮我吗 我的代码如下: public class RoversOnMars { //Variables public int x; // x-coordinate public int

我以前从未使用过JUnit测试。我需要用JUnit测试我的代码。 我已经在谷歌搜索了一整天,但我仍然不知道如何在我的代码中实现它。 例如,我用main方法测试了我的代码。我应该从代码中删除main方法吗? 另外,可能是由于我的错误,我找不到与char变量相关的assert方法。 有人能帮我吗

我的代码如下:

public class RoversOnMars {

    //Variables
    public int x; // x-coordinate 
    public int y;// y-coordinate
    public String compassPoint; // four cardinal compass point,i.e; North, South, West, East
    public static int xCoor, yCoor; // static variables that represent upper-right coordinates of the plateau

    //No-argument constructor
    public RoversOnMars(){
        x = 0;
        y = 0;
        compassPoint = "N";
    }
    // Constructor to set the robotic rover's initial location
    public RoversOnMars(int x, int y, String compassPoint){
        setX(x);
        setY(y);
        this.compassPoint = compassPoint;
    }
    //Accessor method that returns x-coordinate value
    public int getX(){
        return x;
    }
    //Accessor method that returns y-coordinate value
    public int getY(){
        return y;
    }
    //Accessor method that returns compass point value, i.e; North, South, West, East
    public String getCompassPoint(){
        return compassPoint;
    }
    //Mutuator method that sets x-coordinate value
    public void setX(int x){
        this.x = x;
    }
    //Mutuator method that sets y-coordinate value
    public void setY(int y){
        this.y = y;
    }
    //Mutuator method that sets compass point value, i.e; North, South, West, East
    public void setCompassPoint(int number){
        switch(number){
            case 1:
                compassPoint = "N";
                break;
            case 2:
                compassPoint = "S";
                break;
            case 3:
                compassPoint = "W";
                break;
            case 4:
                compassPoint = "E";
                break;
            default:
                System.out.println("Fatal Error.");
                break;
        }
    }
    // Method that includes the letters 'L' for left,  'R' for right, 'M' for move commands.
    //In case of 'L' or 'R' command, the rover does not move; just spin 90 degrees left or right respectively.
    //'M' command makes the rover to move one grid according to where it faces through
    //The method implements the given instructions to tell the rover how to explore the plateau, and prints the instructions in the end.
    public void CommandLetters(String command){
        for(int i=0; i<=command.length()-1; i++){
            switch(command.charAt(i))
            {
                case 'L':
                    if(getCompassPoint() == "N"){
                        setCompassPoint(3);
                        break;
                    }
                    else if(getCompassPoint() == "E"){
                        setCompassPoint(1);
                        break;
                    }
                    else if(getCompassPoint() == "W"){
                        setCompassPoint(2);
                        break;
                    }
                    else if(getCompassPoint() == "S"){
                        setCompassPoint(4);
                        break;
                    }
                case 'R':
                    if(getCompassPoint() == "N"){
                        setCompassPoint(4);
                        break;
                    }
                    else if(getCompassPoint() == "E"){
                        setCompassPoint(2);
                        break;
                    }
                    else if(getCompassPoint() == "W"){
                        setCompassPoint(1);
                        break;
                    }
                    else if(getCompassPoint() == "S"){
                        setCompassPoint(3);
                        break;
                    }
                    break;
                case 'M':
                    if(getCompassPoint() == "N"){
                        this.y = y+1;
                        break;
                    }
                    else if(getCompassPoint() == "E"){
                        this.x = x +1;
                        break;
                    }
                    else if(getCompassPoint() == "W"){
                        this.x = x-1;
                        break;
                    }
                    else if(getCompassPoint() == "S"){
                        this.y = y-1;
                        break;
                    }
                    break;
                default:
                    System.out.println("Fatal Error.");
                    break;
            }
        }
        System.out.println(command);
    }
    //Method to define the upper-right coordinates of the plateau
    public static void setPlateauCoordinate(int a, int b){
        xCoor = a;
        yCoor = b;
    }
}
公共类漫游车{
//变数
公共int x;//x坐标
公共int y;//y坐标
公共字符串罗盘点;//四基数罗盘点,即北、南、西、东
public static int xCoor,yCoor;//表示平台右上角坐标的静态变量
//无参数构造函数
公共漫游车{
x=0;
y=0;
compassPoint=“N”;
}
//建造师设置机器人漫游者的初始位置
公共RoversOnMars(整数x、整数y、字符串圆点){
setX(x);
赛蒂(y);
this.compassPoint=compassPoint;
}
//返回x坐标值的访问器方法
公共int getX(){
返回x;
}
//返回y坐标值的访问器方法
公共int getY(){
返回y;
}
//返回指南针点值的访问器方法,即北、南、西、东
公共字符串getCompassPoint(){
返回罗盘点;
}
//设置x坐标值的Mutuator方法
公共无效集合x(整数x){
这个.x=x;
}
//设置y坐标值的Mutuator方法
公共空间设置(整数y){
这个。y=y;
}
//设置指南针点值的Mutuator方法,即北、南、西、东
公共无效设置点(整数){
开关(编号){
案例1:
compassPoint=“N”;
打破
案例2:
compassPoint=“S”;
打破
案例3:
compassPoint=“W”;
打破
案例4:
compassPoint=“E”;
打破
违约:
System.out.println(“致命错误”);
打破
}
}
//方法,其中包括字母“L”表示左侧,“R”表示右侧,“M”表示移动命令。
//在“L”或“R”指令的情况下,月球车不会移动;只需分别向左或向右旋转90度。
//“M”命令使漫游者根据其面对的位置移动一个网格
//该方法执行给定的指令,告诉漫游者如何探索高原,并最终打印指令。
公共无效命令字母(字符串命令){

对于(inti=0;i来说,很难理解您的代码将要实现什么。但您似乎想要测试名为“CommandLetters”的方法。 网上有很多教程

很少有人是:- 看

查看详细帮助页面

在这里,我分享了非常简单的CommandLetters测试方法(尽管您的代码需要很多重分解)


让我看看我是否能帮你回答你提出的问题

  • “我应该从代码中删除main方法吗?”否。添加Junit后,您仍然可以按照一贯的方式运行代码 (使用主菜单)

    但是,在添加Junit之后,您还可以运行测试 案例。您将有两种方法来运行这些

  • 您的IDE(例如Eclipse)将为您提供一种方法,在添加Junit后,通过转到New->Junit Test Case运行Junit。”
  • 通过指定自定义测试套件类和运行程序来创建命令行
一旦您决定了使用哪种方式(ide与命令行),我可以帮助您轻松地解决这个问题

  • 关于你的第二个问题: 我找不到与char变量相关的assert方法。有人能帮我吗?

    这应该对你有用

    assertArrayEquals(char[] expecteds, char[] actuals) 
         Asserts that two char arrays are equal.
    

我在您的代码中看不到任何与测试相关的内容。有什么内容吗?这是我所有的代码。我不知道我应该根据测试编写它。我应该如何修改它?
assertArrayEquals(char[] expecteds, char[] actuals) 
     Asserts that two char arrays are equal.