Skip to content

Commit

Permalink
Merge pull request springframeworkguru#93 from krzysztof83/master
Browse files Browse the repository at this point in the history
Write CRUD Tests for Pet Map Service, closes springframeworkguru#92
  • Loading branch information
springframeworkguru authored Nov 21, 2018
2 parents 2aeef1e + de3babd commit c8ce750
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ jobs:

# run tests!
- run: mvn integration-test

Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package guru.springframework.sfgpetclinic.services.map;

import guru.springframework.sfgpetclinic.model.Pet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
* JUnit5 test.
*
* @author <a href="mailto:[email protected]">Krzysztof Czechowski</a>
*/

class PetMapServiceTest {

private PetMapService petMapService;

private final Long petId = 1L;

@BeforeEach
void setUp() {

petMapService = new PetMapService();

petMapService.save(Pet.builder().id(petId).build());
}

@Test
void findAll() {

Set<Pet> petSet = petMapService.findAll();

assertEquals(1, petSet.size());
}

@Test
void findByIdExistingId() {

Pet pet = petMapService.findById(petId);

assertEquals(petId, pet.getId());
}

@Test
void findByIdNotExistingId() {

Pet pet = petMapService.findById(5L);

assertNull(pet);
}

@Test
void findByIdNullId() {

Pet pet = petMapService.findById(null);

assertNull(pet);
}

@Test
void saveExistingId() {

Long id = 2L;

Pet pet2 = Pet.builder().id(id).build();

Pet savedPet = petMapService.save(pet2);

assertEquals(id, savedPet.getId());
}

@Test
void saveDuplicateId() {

Long id = 1L;

Pet pet2 = Pet.builder().id(id).build();

Pet savedPet = petMapService.save(pet2);

assertEquals(id, savedPet.getId());
assertEquals(1, petMapService.findAll().size());
}

@Test
void saveNoId() {

Pet savedPet = petMapService.save(Pet.builder().build());

assertNotNull(savedPet);
assertNotNull(savedPet.getId());
assertEquals(2, petMapService.findAll().size());
}

@Test
void deletePet() {

petMapService.delete(petMapService.findById(petId));

assertEquals(0, petMapService.findAll().size());

}

@Test
void deleteWithWrongId() {

Pet pet = Pet.builder().id(5L).build();

petMapService.delete(pet);

assertEquals(1, petMapService.findAll().size());
}

@Test
void deleteWithNullId() {

Pet pet = Pet.builder().build();

petMapService.delete(pet);

assertEquals(1, petMapService.findAll().size());
}

@Test
void deleteNull() {

petMapService.delete(null);

assertEquals(1, petMapService.findAll().size());

}

@Test
void deleteByIdCorrectId() {

petMapService.deleteById(petId);

assertEquals(0, petMapService.findAll().size());
}

@Test
void deleteByIdWrongId() {

petMapService.deleteById(5L);

assertEquals(1, petMapService.findAll().size());
}

@Test
void deleteByIdNullId() {

petMapService.deleteById(null);

assertEquals(1, petMapService.findAll().size());
}
}

0 comments on commit c8ce750

Please sign in to comment.