Skip to content

mtumilowicz/testing-junit4-lab1

Repository files navigation

Build Status

testing-junit4-lab1

Junit4 basics training exercises.

manual

  1. Open class Person
  2. Ctrl + Shift + T
  3. Create New Test -> OK -> OK
  4. In PersonTest class:
    • create methods setAgeTest
    • put @Test annotation over it
    • create object Person
    • person.setAge(10)
    • verify if person.getAge() is 10
      @Test
      public void setAgeTest() {
      // given
          Person person = new Person();
          
      // when    
          person.setAge(10);
          
      // then
          assertThat(person.getAge(), is(10));
      }    
      
      imports:
      import static org.hamcrest.CoreMatchers.is;
      import static org.junit.Assert.*;
      
    • Verifying exceptions:
      @Test(expected = IllegalArgumentException.class)
      public void setAgeTest() {
      // given
          Person person = new Person();
          
      // when
          person.setAge(-4);
      }    
      

tasks

  1. Test class entities.Person
    • constructor - OK
    • constructor - exceptions (parameter validation)
    • setters + getters - OK
    • setters - exceptions (parameter validation)
    • addAllHobbiesTo - no common hobbies
    • addAllHobbiesTo - some common hobbies
    • addHobby - add already existing
    • addHobby - add new
  2. Test class entities.PersonImmutable
    • constructor - OK
    • constructor - exceptions (parameter validation)
    • check if hobbies set is immutable (try to add something)
    • check equals contract
    • check hashcode contract
  3. Test class service.PersonImmutableService
    • checkIfAnyHasGivenName - OK
    • checkIfAnyHasGivenName - exception (parameter validation)
    • findAllOlderThanGivenAge - OK

To read