Reputation:
My first method is simple, its just a toString
which outputs 3 class specific fields
public String toString(){
return String.format("%s/%s/%s",street,city,postcode);
}
In JUnit it gives me this to form the tests on, I am unsure how to do this efficiently.
@Test
public void testToString() {
System.out.println("toString");
Address instance = null;
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
I have done lots of research on JUnit and understand the concept of testing but can't quite get my head around how to do it with my code. Also how would I test this method efficiently, this is a method in a subclass which is the abstract method in the parent class
@Override
int getDiscountRate() {
return this.companyDiscount;
}
It gives me this to test
@Test
public void testGetDiscountRate() {
System.out.println("getDiscountRate");
BusinessOrganisationDetails instance = null;
int expResult = 0;
int result = instance.getDiscountRate();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
Upvotes: 1
Views: 323
Reputation: 937
This looks good. Of course, you need to instanciate the instance variables, else it will result in a NullPointerException.
Here, how it could look like in the end:
@Test
public void testToString() {
System.out.println("toString");
Address instance = new Address();
instance.setStreet("Somestreet");
instance.setCity("Somecity");
instance.setPostcode("12345");
String expResult = "SomestreetSomecity12345";
String result = instance.toString();
assertEquals(expResult, result);
}
And the other test:
@Test
public void testGetDiscountRate() {
System.out.println("getDiscountRate");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails();
instance.setCompanyDiscount(50);
int expResult = 50;
int result = instance.getDiscountRate();
assertEquals(expResult, result);
}
Upvotes: 1