Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

how to test mutators, constructors, comparable, toString, Exceptions using junit in java

Hi,


how to test mutators, constructors, comparable interface(say i have Employee objects with FirstName, LastName, Age etc to compareTo()), toString, Exceptions, equals()(how assertTrue different from assertEquals()), objects in equality using junit in java. Why junit onwards methods do not need to prefix with testXyz() like that. Please advise
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

assertTrue asserts that the boolean expression is true, for example (0<size && size<=max)
assertEquals compares the parameters for equality

Here are some tests for an Employee object.
//The Employee class must have a method named equals which takes an Employee object as a parameter 
//and compares the member variables for equality.
Employee first = new Employee("John", "Doe", 43);
Employee second = new Employee("John", "Doe", 43);
assertTrue(first.equals(second));

//The Employee class must have a method named toString which creates a string from the member variables
assertEquals("John Doe, age 43", first.toString();

Open in new window


Use the @Test annotation before each test method. It doesn't matter what the method is named.
@Test
public void myTestMethod() {
   //assert some condition
}

Open in new window

Avatar of gudii9

ASKER

//The Employee class must have a method named equals which takes an Employee object as a parameter 
//and compares the member variables for equality.
Employee first = new Employee("John", "Doe", 43);
Employee second = new Employee("John", "Doe", 43);
assertTrue(first.equals(second));

//The Employee class must have a method named toString which creates a string from the member variables
assertEquals("John Doe, age 43", first.toString();

Open in new window

i wonder what
assertTrue(first.equals(second));
and
assertEquals("John Doe, age 43", first.toString();
prints in this case. please advise
ASKER CERTIFIED SOLUTION
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial