|
| 1 | +package com.codedifferently.lesson16.hummadtanweer; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.junit.jupiter.api.Assertions.fail; |
| 7 | + |
| 8 | +import com.codedifferently.lesson16.hummadtanweer.Person.Gender; |
| 9 | +import com.codedifferently.lesson16.hummadtanweer.Person.HobbyLimitExceededException; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class PersonTest { |
| 13 | + @Test |
| 14 | + void testIsAdult() { |
| 15 | + Person adult = new Person( "xyz", 29, Gender. MALE, "[email protected]"); |
| 16 | + assertTrue(adult.isAdult(), "True for 29"); |
| 17 | + Person minor = new Person( "abc", 15, Gender. FEMALE, "[email protected]"); |
| 18 | + assertFalse(minor.isAdult(), "False for 15"); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + void testAddHobby() throws HobbyLimitExceededException { |
| 23 | + Person person = new Person( "xyz", 29, Gender. MALE, "[email protected]"); |
| 24 | + |
| 25 | + person.addHobby("Football"); |
| 26 | + person.addHobby("Pickleball"); |
| 27 | + |
| 28 | + assertEquals(2, person.getHobbies().size()); |
| 29 | + assertTrue(person.getHobbies().contains("Football")); |
| 30 | + assertTrue(person.getHobbies().contains("Pickleball")); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testGetName() { |
| 35 | + Person person = new Person( "xyz", 29, Gender. MALE, "[email protected]"); |
| 36 | + assertEquals("xyz", person.getName()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void getEmail() { |
| 41 | + Person person = new Person( "xyz", 29, Gender. MALE, "[email protected]"); |
| 42 | + assertEquals( "[email protected]", person. getEmail()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testAddHobbyExceedsLimit() throws HobbyLimitExceededException { |
| 47 | + Person person = new Person( "xyz", 29, Gender. MALE, "[email protected]"); |
| 48 | + |
| 49 | + try { |
| 50 | + person.addHobby("Football"); |
| 51 | + person.addHobby("Pickleball"); |
| 52 | + person.addHobby("Basketball"); |
| 53 | + fail("Expected HobbyLimitExceededException to be thrown"); |
| 54 | + } catch (HobbyLimitExceededException e) { |
| 55 | + assertEquals("Cannot add more than 2 hobbies.", e.getMessage()); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments