Java JUnit比较字符串

Java JUnit比较字符串,java,junit,Java,Junit,在JUnit上做作业。我们必须测试我们所有的方法。我已经写出了其他4种方法并进行了正确的测试,但是我遇到了toString方法无法正常工作的问题 //here is my Gps constructor, if needed. I don't think it is, but including it just in case. public Gps(String n, GpsCoordinates pos) { super(); this.name =

在JUnit上做作业。我们必须测试我们所有的方法。我已经写出了其他4种方法并进行了正确的测试,但是我遇到了toString方法无法正常工作的问题

//here is my Gps constructor, if needed. I don't think it is, but including it just in case.

public Gps(String n, GpsCoordinates pos)
    {
        super();
        this.name = n;
        this.position = pos;
    }

//Here is my Gps.class toString() method that I want to test.
@Override
public String toString()
    {
        String gps = "myGPS: " + name + ": " + position;
        return gps;
    }
以下是我的JUnit测试方法:

//Here are my instances in my GpsTest.class

private GpsCoordinates testGpsCoordinates = new GpsCoordinates(40.760671, -111.891122);
private Gps testGps3 = new Gps("TEST3", testGpsCoordinates);

//Here is my toString test method
    @Test
    public void testToString()
        {
            String expected = "myGPS: TEST3: 40.760671, -111.891122";
            assertEquals(expected, testGps3.toString());
        }
所以当我运行这个程序时,我会遇到JUnit失败。我查了一下日志,上面写着:

Expected:
myGPS: TEST3: 40.760671, -111.891122

Actual:
myGPS: TEST3: 40.760671, -111.891122
我想assertEquals可能使用“==”而不是.equals(),但事实并非如此——它确实使用.equals(),所以我没有主意了

谁能给我指出正确的方向吗?现在,我已经在这个问题上纠结了30分钟了,到处移动我的实例,重命名东西,等等,现在我正在把头发拔出来

cricket_007要求我添加我的GpsCoordinates.toString(),这是:

public String toString()
{
    //formatting this to only return to 6 decimal places. On a separate part
    //of the assignment, we are to add a random double to the GpsCoordinates
    //(like a fake "gps update") and limit it to 6 decimal places.
    DecimalFormat df = new DecimalFormat("####.000000");
    return df.format(latitude) + ", " + df.format(longitude) + "\n";
}

当GpsCoordinate.toString()添加它时,预期值没有“\n”。

当GpsCoordinate.toString()添加它时,预期值没有“\n”。

因此可能导致此问题的一个原因是意外的尾随空白

你有两个选择来处理这个问题:

  • 在eclipse或类似IDE中运行可能会让您可以通过双击故障来获取更多信息。在eclipse中,对于
    assertEquals
    failures,您会得到两个字符串的差异-这以一种明显的方式显示了差异

  • 在比较之前在字符串中添加哨兵:例如

    assertEquals("'myGPS: TEST3: 40.760671, -111.891122'",
                 "'" + testGps3.toString() +"'")
    
    现在,任何空白区差异都应该更加明显

  • 正如Mateusz Mrozewski提到的,您的输出中有一个额外的换行符。在这种情况下,输出为2。将是

    Expected:
    'myGPS: TEST3: 40.760671, -111.891122'
    
    Actual:
    'myGPS: TEST3: 40.760671, -111.891122
    '
    

    问题是显而易见的。

    因此,可能导致此问题的一个原因是意外的尾随空格

    你有两个选择来处理这个问题:

  • 在eclipse或类似IDE中运行可能会让您可以通过双击故障来获取更多信息。在eclipse中,对于
    assertEquals
    failures,您会得到两个字符串的差异-这以一种明显的方式显示了差异

  • 在比较之前在字符串中添加哨兵:例如

    assertEquals("'myGPS: TEST3: 40.760671, -111.891122'",
                 "'" + testGps3.toString() +"'")
    
    现在,任何空白区差异都应该更加明显

  • 正如Mateusz Mrozewski提到的,您的输出中有一个额外的换行符。在这种情况下,输出为2。将是

    Expected:
    'myGPS: TEST3: 40.760671, -111.891122'
    
    Actual:
    'myGPS: TEST3: 40.760671, -111.891122
    '
    

    这个问题很明显。

    添加
    GpsCoordinates.toString
    ,拜托?@cricket\u 007我在原来的帖子中添加了这个。您是否认为我的GpsCoordinates.toString()会被调用而不是Gps.toString()?当您使用position变量进行字符串连接时会调用它。这就是我为什么问的原因。@cricket\u 007你说得对,我很笨“位置”属于Gps坐标,而不是Gps。谢谢你,板球!是的,你在expectedAdd
    GpsCoordinates的结尾处缺了一行新词。toString
    ,拜托?@cricket_007我在原来的帖子中加了这一行。您是否认为我的GpsCoordinates.toString()会被调用而不是Gps.toString()?当您使用position变量进行字符串连接时会调用它。这就是我为什么问的原因。@cricket\u 007你说得对,我很笨“位置”属于Gps坐标,而不是Gps。谢谢你,板球!是的,你错过了expectedI结尾的一个新行,我实际上在寻找一种方法来做到这一点,所以我将来肯定会使用它。谢谢你,迈克尔。实际上我正在寻找一种方法来做到这一点,所以我将来肯定会使用它。谢谢你,迈克尔。