Aaron
Aaron

Reputation: 999

Using a class to test another class?

For the given class below, I was asked to test this class with a new TimeTest class that has a method doTest() to try out these methods in my Time class. I have tested classes before via the main method, but I have no idea how to test a class using a class. Could anyone help me get started?

public class Time {

    private int minute;
    private int hour;
    private int totalMinute;

    public Time() {
        minute = 0;
        hour = 0;
    }

    public Time(int hours, int minutes) {
        setHour(hours);
        setMinute(minutes);
    }

    private void setHour(int hours) {
        if (hours < 24 && hours >= 0) {
            hour = hours;
        } else {
            hour = 0;
        }
    }

    private void setMinute(int minutes) {
        if (minutes < 60 && minutes >= 0) {
            minute = minutes;
        } else {
            minute = 0;
        }
    }

    public void setTime(int hours, int minutes) {
        setHour(hours);
        setMinute(minutes);
    }

    public int getElapsedTime(Time that) {
        int thisTime = this.getTotalMinutes();
        int thatTime = that.getTotalMinutes();
        if (thisTime > thatTime) {
            return thisTime - thatTime;
        }
        return thatTime - thisTime;
    }

    private int getTotalMinutes() {
        totalMinute = (hour * 60) + minute;

        return totalMinute;
    }

    public String getAsString() {
        if (hour < 10 && minute < 10) {
            return "0" + hour + ":0" + minute;
        } else if (hour < 10 && minute >= 10) {
            return "0" + hour + ":" + minute;
        } else if (hour >= 10 && minute < 10) {
            return hour + ":0" + minute;
        } else {
            return hour + ":" + minute;
        }
    }
}

Upvotes: 0

Views: 1787

Answers (7)

Bilal
Bilal

Reputation: 1

public class TimeTest
{

  public static void main(String args[])
  {
   doTest();

  }

  public static void doTest()
  {
   Time t1=new Time();

   Time t2=new Time(12,45);

   System.out.printf("%s\n",t1.getAsString());
   System.out.printf("%s",t2.getAsString());

  }


}// end of class TimeTest

That is how you can try other methods of class time in method doTest of class TimeTest

Upvotes: 0

Makoto
Makoto

Reputation: 106390

Take a look at JUnit. It can provide you with an easy-to-use testing environment.

The main concept when you're testing your code - a test should only ever assert one thing. What that thing is depends on your code.

Here's an example of a test on your current class. I wish to assert that, after setting your Time class to 1:13, I will return that exact String. This test should live in its own class, independent of your Time object.

@Test
public void testGetAsString() {
    Time testObj = new Time();
    String expected = "01:13";
    testObj.setTime(1, 13);
    String result = testObj.getAsString();
    Assert.assertEquals("the time isn't properly rendered", result, expected);
}

Unit tests like this can greatly help you with larger projects, refactoring, and is one of the three parts of test-driven development.

If you're interested, you can also look into other testing frameworks which mimic, or mock, the behavior of another object. EasyMock, PowerMock, and Mockito can help you in that regard.

Upvotes: 1

Heggi
Heggi

Reputation: 504

Here is the class which may meet your requirement. If you do not want main method in this class then write the main method in another class and Create object of TimeTest and call the doTest(time) method from that class which has main method.

public class TimeTest {

public static void main(String[] args) {
    Time time = new Time();
    time.setTime(10, 10);
    doTest(time);

}

private static void doTest(Time time) {
    //set the time to 10 hours and 10 minutes
    System.out.println("Get as String: " + time.getAsString());
    // set the new time to test the getElapsedTime(time) method
    Time newTime = new Time(11, 30);
    System.out.println("Get Elapsed Time: " + time.getElapsedTime(newTime) + " minutes");
}

}

Thanks

Upvotes: 2

code_blue
code_blue

Reputation: 256

You should check out JUnit framework.

The test class should look like this:

public class TimeTest {

    public TimeTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test

    public void doTest()
    {
        Time instance = new Time();
        //your code to test the methods
        //use assertEquals(expectedResult, result) to compare the result you expected                with the result from your code  
    }

}

Upvotes: 0

mcalex
mcalex

Reputation: 6778

The simple way is to do whatever you did in main() in the new Test class.

So, if previously, main created a new object of the class and then called methods on that object, your Test class would create a new object of the original class and call its methods.

Something like:

public class TimeTest
{
   public static void main(String [] args)
   {
      Time timeA = new Time();
      Time timeB = new Time(12, 30);
      System.out.println("Time Diff: " + timeB.getElapsedTime(timeA);
      //  etc
   }
}

Once you've got this, investigate the different testing frameworks, like JUnit, which automatically deal with the administrative side of testing, allowing you to do tests like you do code.

Upvotes: 0

Debobroto Das
Debobroto Das

Reputation: 862

Write another testclass inside that class use a main function. and from that main function test the class.

You need to test your time class? am I right?

 public class test
 {
     public static void main(String args[])
     {
        Time t = new Time();

       //now use any method of time class
        //i.e. t.methodname(pram)
     }
 }

Upvotes: -1

someone
someone

Reputation: 6572

Junit is the best place to start testing your logics.

Upvotes: 1

Related Questions