My assertEquals is returning the package name when I run a Maven Test

Viewed 16

I have a TestEntry Class:

@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");
    }

and the actual Entry Class:

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

The Test for testFloatConstructor() works fine but when I run testSymbolConstructor(), the expected just returns my package name (The actual is correct, returning -). What is causing this? Am I not constructing the symbol class correctly:

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

I haven't finished the Symbol Class yet as I'm programming with TDD and trying to get this Test to pass first. Even with hard-coding/cheating I can't get it to work.

Thanks for any help!

1 Answers

In this line:

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

you are comparing symbolTest.other, which is of type Symbol to Symbol.MINUS, which is type String. You probably want to write:

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

And FYI, first argument to assertEquals is expected value, and second is actual value - that's why error message might be confusing for you.

Related