【问题标题】:My assertEquals is returning the package name when I run a Maven Test当我运行 Maven 测试时,我的 assertEquals 正在返回包名称
【发布时间】:2022-01-16 00:29:36
【问题描述】:

我有一个 TestEntry 类:

@Test
void testFloatConstructor() {
    Entry floatTest = new Entry(1);
    assertEquals(floatTest.number, 1, "Test that the constructor sets 
       the Entry float to 1");
}
    
@Test
void testSymbolConstructor() {
    Symbol symbolTestSign = new Symbol(Symbol.MINUS);
    Entry symbolTest = new Entry(symbolTestSign);
    assertEquals(symbolTest.other, Symbol.MINUS, "Test that the 
        constructor sets the Symbol to - sign");
    }

以及实际的Entry Class:

float number;
Symbol other;
String str;
Type type;
    
public Entry(float value) {
    this.number = value; 
}
    
public Entry(Symbol which) {
    this.other = which;
}

testFloatConstructor() 的测试工作正常,但是当我运行 testSymbolConstructor() 时,预期 只返回我的包名(实际 是正确的,返回 -)。这是什么原因造成的?我没有正确构造符号类吗:

enum Symbols {
    LEFT_BRACKET,
    RIGHT_BRACKET,
    TIMES,
    DIVIDE,
    PLUS,
    MINUS,
    INVALID
}

public static final String MINUS = "-";
public static final String PLUS = "+";
String symbol;
    
public Symbol() {
    
}
    
public Symbol(String symbol) {
    this.symbol = symbol;
}

我还没有完成符号类,因为我正在使用 TDD 编程并试图让这个测试首先通过。即使使用硬编码/作弊,我也无法让它发挥作用。

感谢您的帮助!

【问题讨论】:

    标签: import enums constructor tdd assert


    【解决方案1】:

    在这一行:

    assertEquals(symbolTest.other, Symbol.MINUS, "Test that the 
            constructor sets the Symbol to - sign");
    

    您正在将symbolTest.other(类型为Symbol)与Symbol.MINUS(类型为String)进行比较。你可能想写:

    assertEquals(symbolTest.other.symbol, Symbol.MINUS, "Test that the 
            constructor sets the Symbol to - sign");
    

    仅供参考,assertEquals 的第一个参数是预期值,第二个是实际值 - 这就是错误消息可能会让您感到困惑的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-09
      • 2021-01-16
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多