user2009481
user2009481

Reputation: 5

I fail junit tests because of return type (I think)

So I have a class full of junit tests and a class full of methods that perform binary operations. The tests are checking to see if I have the right values at certain points.

I am failing a lot of tests because of what I believe to be is the return type. For example I get the message

junit.framework.ComparisonFailure: null expected:<[000]> but was <[BinaryNumber@4896b555]>

If I'm understanding this it's saying that it was looking for an array containing 000 but it got a BinaryNumber (which is the required return type). To help clarify here is one of the methods.

    public BinaryADT and(BinaryADT y) {
    int[] homeArr = pad(((BinaryNumber) y).getNumber());
    int[] awayArr = ((BinaryNumber) y).pad(getNumber());

    int[] solution = new int[awayArr.length];
    int i = 0;
    String empty = "";

    while(i < solution.length){
        solution[i] = homeArr[i] & awayArr[i];
        i++;
    }
    for(int indexF = 0; indexF < solution.length; indexF++){
        empty = empty + solution[indexF];
    }
    System.out.println(empty);

    return new BinaryNumber(empty);
}

Am I understanding this right? If not could someone please explain? I'd also like to point out that this is for my homework but I'm not asking for answers/someone to do it for me. Just a point in the right direction at most.

I will gladly clarify more if it is needed (I didn't want to bog everything down).

Also this is my first post on here. I tried to keep to the formatting suggestions but I apologize if anything is sub-par.

As suggested here is the test method

public void testAnd1()
{
    BinaryADT x = new BinaryNumber("111");
    BinaryADT y = new BinaryNumber("000");
    BinaryADT z = x.and(y);
    assertNotSame(x,z);
    assertNotSame(y,z);
    assertEquals("000",z.toString());
}

Upvotes: 0

Views: 467

Answers (2)

Yogesh Ralebhat
Yogesh Ralebhat

Reputation: 1466

Whenever you see the output of "toString()" like ClassName@SomeNumber, then you can be sure that toString() method is not implemented for that class (unless toString() method implementation itself is not like this).

In your case, expected value is [000], but you are getting [BinaryNumber@4896b555].

Try to implement toString() method in BinaryNumber class and return the value from this method as per assertEquals() expects. This should solve the problem.

Upvotes: 1

William Feirie
William Feirie

Reputation: 644

Can you show me your test code?

1.Your expected type is different from the actual type.

2.BinaryADT class didn't overide toString method.

Upvotes: 0

Related Questions