Java JUnit 测试

Java JUnit Testing

我从未使用过 JUnit 测试 before.I 需要使用 JUnit 测试我的代码。 我一直在搜索 google 一整天,但我仍然不知道如何将它实现到我的代码中。 例如,我用 main method.should 测试了我的代码 我从我的代码中删除了 main 方法? 另外,可能由于我的错误,我找不到与 char 变量相关的断言方法。 谁能帮我解决这个问题?

我的代码如下:

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

真的很难理解你的代码要实现什么。但似乎您想测试名为“CommandLetters”的方法。 网上有很多教程。

少数是:- 参见 tutorialspoints

查看Official JUnit 页详细帮助

http://www.vogella.com/tutorials/JUnit/article.html

在这里,我将分享非常简单的 CommandLetters 测试方法(尽管您的代码需要大量重构)。您可以添加更多测试用例和其他各种 assetions

package com.core.java.testing;  

import org.junit.After;  
import org.junit.Before;  
import org.junit.Test;  

import static org.junit.Assert.*;  

public class RoversOnMarsTest {  

// Method is called before each test case execution  
@Before
public void setUp() throws Exception {

}

// Method is called after each test case execution
@After
public void tearDown() throws Exception {

}

@Test
public void testCommandLettersDefaultConst() throws Exception {
    // default const x=0, y=0 , compass : N
    RoversOnMars roversOnMars = new RoversOnMars();
    roversOnMars.CommandLetters("M");
    assertEquals("Y should be 1", roversOnMars.getY(), 1);
    assertEquals("X should be 1", roversOnMars.getX(), 0);
}

@Test
public void testCommandForWCompass() throws Exception {
    // default const x=0, y=0 , compass : W
    RoversOnMars roversOnMars = new RoversOnMars(23, 23, "W");
    roversOnMars.CommandLetters("M");
    assertEquals("Y should be 23", roversOnMars.getY(), 23);
    assertEquals("X should be 22", roversOnMars.getX(), 22);
   }
 }

让我看看是否可以帮助解决您提出的问题。你问

  • "Should I remove the main method from my code?" 不。一旦你添加了 Junit,你仍然可以 运行 你的代码像你一直那样 运行它 (使用主要)

    但是在你加入 Junit 之后你也可以 运行 测试 个案。您将有 2 种方法 运行 这些

    1. 您的 IDE(例如 Eclipse)将为您提供一种方法来 运行 一旦您通过转到新建 -> Junit 测试用例添加 Junits。"
    2. 通过指定自定义测试套件 class 和 运行ner 的命令行。

一旦你确定ide你想用哪种方式(ide vs 命令行),我可以帮助你轻松解决这个问题。

  • 关于你的第二个问题: 我找不到与 char 变量相关的断言方法。谁能帮我解决这个问题?

    这应该适合你

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