M Cello
M Cello

Reputation: 42

Testing method which outputs an object using Junit

As of today I've been researching unit testing and came across an obstacle. I have a given method which I want to test with an unittest, the method may or may not have errors. The method is this one:

@Override
public ITimeSpan intersectionWith(ITimeSpan timeSpan) {

    ITime begintime, endtime;
    if (bt.compareTo(timeSpan.getBeginTime()) > 0) {
        begintime = bt;
    } else {
        begintime = timeSpan.getBeginTime();
    }

    if (et.compareTo(timeSpan.getEndTime()) < 0) {
        endtime = et;
    } else {
        endtime = timeSpan.getEndTime();
    }
    // aangepast van >= naar <= 
    if (begintime.compareTo(endtime) <= 0) {
        return null;
    }

    return new TimeSpan(begintime, endtime);
}

This method is supposed to output a new timespan constructed of overlapping time of two timespans. I've written a unit test for this method which looks like this:

@Test
public void testIntersectionWith() {
    System.out.println("intersectionWith");
    ITimeSpan timeSpan = new TimeSpan(new Time (2000,4,4,4,4), new Time(2000,5,5,5,5));
    TimeSpan instance3 = new TimeSpan(new Time (2000,4,4,4,4), new Time(2000,8,8,8,8));
    ITimeSpan expResult3 = new TimeSpan(new Time (2000,4,4,4,4), new Time(2000,5,5,5,5));
    ITimeSpan result3 = instance3.intersectionWith(timeSpan);


    assertEquals( result3, expResult3);

     }

In order to compare to objects with each other I've overridden the equals method in the TimeSpan class like this:

@Override
public boolean equals(Object obj){
        if (obj == null){
            return false;
        }
        final TimeSpan other = (TimeSpan) obj;
        if (this.bt == other.bt && this.et == other.et){
            return true;
        } 
        return false;
}

Looking at the given timespans in the test I expected the test to pass, but as you can guess it didn't and it returns an "expected: but was " error. And points to the line of code which says:

assertEquals( result3, expResult3);

I'm trying to understand why this comparison with assertEquals is not working, it looks like overriding the equals method is not working here. I've also tried to override hashCode() method but that doesn't seem to make a difference.

Upvotes: 1

Views: 205

Answers (3)

Bhaskara Arani
Bhaskara Arani

Reputation: 1677

@M Cello, i think culprit will be here "return new TimeSpan(begintime, endtime);"

public ITimeSpan intersectionWith(ITimeSpan timeSpan) {

ITime begintime, endtime;
if (bt.compareTo(timeSpan.getBeginTime()) > 0) {
    begintime = bt;
} else {
    begintime = timeSpan.getBeginTime();
}

if (et.compareTo(timeSpan.getEndTime()) < 0) {
    endtime = et;
} else {
    endtime = timeSpan.getEndTime();
}
// aangepast van >= naar <= 
if (begintime.compareTo(endtime) <= 0) {
    return null;
}
//i think the culprit is here, plz print the begintime, endtime and check what are the values its passing other than this everything is working fine
return new TimeSpan(begintime, endtime);
}

Upvotes: 1

Nosretep
Nosretep

Reputation: 17

I see several concerns. First, compare equality using .equals(), not == in your equals method. Second, not something that will cause an error now, but you need to type check in your equals method or you can get a CCE. I recommend you have your ID auto generate the equals method. FYI, that should also create a hashCode method, which you are missing.

Upvotes: 1

talex
talex

Reputation: 20543

Your equals method is wrong you use == in it.

Try this:

    if (this.bt.equals( other.bt) && this.et .equals( other.et)){

Upvotes: 1

Related Questions