当我运行Maven测试时,我的assertEquals返回包名称



我有一个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");
}

以及实际的入门级:

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编程,并试图让这个测试首先通过。即使是硬编码/作弊,我也无法让它发挥作用。

谢谢你的帮助!

在行中:

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

您正在比较类型为SymbolsymbolTest.other和类型为StringSymbol.MINUS。你可能想写:

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

仅供参考,assertEquals的第一个参数是预期的值,第二个参数是实际的

最新更新