I am currently learning how to write tests to Java-code with JUnit.
Is the setup of the tests and syntax of this test okay? And should I keep all test-methods for one class in the same test-class?
Hope somebody can give some feedback!
public class HotelTest {
Hotel hotel;
int id1=0;
int id2=0;
@Before
public void setUp(){
hotel = new Hotel();
this.id1 = hotel.registerRoom(5, "lowpriceroom", 200);
this.id2 = hotel.registerRoom(3, "qualityroom", 300);
}
@After
public void tearDown(){
hotel.hotelRooms.clear();
}
@Test
public void testRegisterRoom() {
System.out.println(hotel.hotelRooms);
assertTrue(hotel.hotelRooms.size() == 2);
assertEquals(200, hotel.hotelRooms.get(id1).getPrice());
assertEquals(3, hotel.hotelRooms.get(id2).getNumberOfPeople());
}
@Test(expected = IllegalArgumentException.class)
public void testExceptionWrongRoom() {
hotel.registerRoom(3, "wrongRoom", 200);
}
@Test(expected = IllegalArgumentException.class)
public void testExceptionTooManyPeople() {
hotel.registerRoom(11, "lowpriceroom", 200);
}
This is the method i am testing:
static HashMap<Integer, Room> hotelRooms = new HashMap<Integer, Room>();
public int registerRoom(int numberOfPeople, String roomtype, int price){
//Checking for illegal input
if(numberOfPeople > 10){
throw new IllegalArgumentException("Error! Too many persons!");
}
if(roomtype == "lowpriceroom" ){
Lowpriceroom newRoom = new Lowpriceroom(numberOfPeople, price);
hotelRooms.put(newRoom.getID(), newRoom);
return newRoom.getID();
}
else if(roomtype == "qualityroom" ){
Qualityroom newRoom = new Qualityroom(numberOfPeople, price);
hotelRooms.put(newRoom.getID(), newRoom);
return newRoom.getID();
}
else{
throw new IllegalArgumentException("Error! Not valid roomtype!");
}
Aucun commentaire:
Enregistrer un commentaire