Reputation: 7
I need to write unit tests for my methods. I'm having a bit of trouble because I'm new to JUnit. I need to write a test for a getter method of an object type I created. The object type is UnitInfo, and I need to write a test for the method
@Override
public UnitInfo getInfo() {
return info;
}
in the class building. I put my building class, UnitInfo class and my buildingTest class in the code below. Any help is appreciated.
package main.model.facility;
import java.util.List;
public class UnitInfo {
private int capacity;
private String name;
private int idNumber;
private List<String> details;
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public List<String> getDetails() {
return details;
}
public void setDetails(List<String> details) {
this.details = details;
}
public void addDetail(String detail) {
details.add(detail);
}
public void removeDetail(String detail) {
details.remove(detail);
}
}
Building class:
package main.model.facility;
import java.util.List;
public class Building extends Facility {
private List<IFacility<UnitInfo>> subunits;
private UnitInfo info;
private ScheduleManager schedule;
@Override
public UnitInfo getInfo() {
return info;
}
@Override
public ScheduleManager getScheduleManager() {
return schedule;
}
@Override
public List<IFacility<UnitInfo>> listFacilities() {
return subunits;
}
@Override
public int requestAvailableCapacity() {
int availableCapacity = 0;
for (IFacility<UnitInfo> subunit : subunits){
availableCapacity += subunit.requestAvailableCapacity();
}
return availableCapacity;
}
}
Junit
public class BuildingTest {
Building defaultBuilding = new Building();
ScheduleManager defaultSchedule = new ScheduleManager();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testGetInfo() { //this is the test I need to write
Building b = defaultBuilding;
assertEquals(b.getInfo(), null);
}
@Test
public void testGetScheduleManager() {
Building a = defaultBuilding;
assertEquals(a.getScheduleManager(), null);
}
Upvotes: 0
Views: 2113
Reputation: 3760
Don't build your Building
at the class level, create it inside the unit test so that each test has its own Building
to work with. This way they won't interfere with each other.
Your test seems fine at the moment, you don't initialize info
when you construct a new Building
so info
is null, and you are asserting that in your unit test. You could also use assertNull(b.getInfo());
.
@Test
public void testGetInfo() { //this is the test I need to write
Building b = new Building()
assertEquals(b.getInfo(), null);
}
If you did initialize info
to something else, say info = new UnitInfo()
, then you could change your test to:
@Test
public void testGetInfo() { //this is the test I need to write
Building b = new Building()
assertNotNull(b.getInfo());
}
How about if, when you create a new Building
you initialized info
and set some of the fields.
info = new UnitInfo();
info.setIdNumber(100);
info.setName("Some Unit Info");
Then in your unit test you could assert that the fields were set:
@Test
public void testGetInfo() { //this is the test I need to write
Building b = new Building()
assertNotNull(b.getInfo());
assertEquals(b.getInfo().getIdNumber(), 100);
assertEquals(b.getInfo().getName(), "Some Unit Info");
}
The idea with unit tests is to exercise your code; call your methods and then assert that the results are what you expect. Once you have a good base of unit tests against all your code, you can feel confident about modifying it because you know that your tests will tell you when you break something.
Keep your tests small and simple, don't do too much. Just do your setup, call a method and make sure the results are correct. Then write a new test, and do something else. Don't write tests that do 5 different things.
Upvotes: 1