From be403a30177476529a8d58d384807c6148e5b6bc Mon Sep 17 00:00:00 2001 From: Mic <misvy@vmware.com> Date: Tue, 26 Feb 2013 17:20:55 +0800 Subject: [PATCH] migrate all JUnit Integration tests from the repo layer to the service layer (step 1) --- .../AbstractOwnerRepositoryTests.java | 30 ++++++++-------- .../AbstractPetRepositoryTests.java | 34 ++++++++----------- .../AbstractVetRepositoryTests.java | 17 +++++----- .../AbstractVisitRepositoryTests.java | 16 ++++----- 4 files changed, 45 insertions(+), 52 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/repository/AbstractOwnerRepositoryTests.java b/src/test/java/org/springframework/samples/petclinic/repository/AbstractOwnerRepositoryTests.java index e8330df..57a5784 100644 --- a/src/test/java/org/springframework/samples/petclinic/repository/AbstractOwnerRepositoryTests.java +++ b/src/test/java/org/springframework/samples/petclinic/repository/AbstractOwnerRepositoryTests.java @@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -27,13 +28,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** - * <p> Base class for {@link OwnerRepository} integration tests. </p> <p> Subclasses should specify Spring context + * <p> Base class for {@link clinicService} integration tests. </p> <p> Subclasses should specify Spring context * configuration using {@link ContextConfiguration @ContextConfiguration} annotation </p> <p> - * AbstractOwnerRepositoryTests and its subclasses benefit from the following services provided by the Spring + * AbstractclinicServiceTests and its subclasses benefit from the following services provided by the Spring * TestContext Framework: </p> <ul> <li><strong>Spring IoC container caching</strong> which spares us unnecessary set up * time between test execution.</li> <li><strong>Dependency Injection</strong> of test fixture instances, meaning that * we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the <code>{@link - * AbstractOwnerRepositoryTests#ownerRepository ownerRepository}</code> instance variable, which uses autowiring <em>by + * AbstractclinicServiceTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by * type</em>. <li><strong>Transaction management</strong>, meaning each test method is executed in its own transaction, * which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there * is no need for a teardown or cleanup script. <li> An {@link org.springframework.context.ApplicationContext @@ -48,23 +49,22 @@ import static org.junit.Assert.assertTrue; public abstract class AbstractOwnerRepositoryTests { @Autowired - protected OwnerRepository ownerRepository; + protected ClinicService clinicService; @Test @Transactional public void findOwners() { - Collection<Owner> owners = this.ownerRepository.findByLastName("Davis"); + Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis"); assertEquals(2, owners.size()); - owners = this.ownerRepository.findByLastName("Daviss"); + owners = this.clinicService.findOwnerByLastName("Daviss"); assertEquals(0, owners.size()); } @Test - @Transactional public void findSingleOwner() { - Owner owner1 = this.ownerRepository.findById(1); + Owner owner1 = this.clinicService.findOwnerById(1); assertTrue(owner1.getLastName().startsWith("Franklin")); - Owner owner10 = this.ownerRepository.findById(10); + Owner owner10 = this.clinicService.findOwnerById(10); assertEquals("Carlos", owner10.getFirstName()); assertEquals(owner1.getPets().size(), 1); @@ -73,7 +73,7 @@ public abstract class AbstractOwnerRepositoryTests { @Test @Transactional public void insertOwner() { - Collection<Owner> owners = this.ownerRepository.findByLastName("Schultz"); + Collection<Owner> owners = this.clinicService.findOwnerByLastName("Schultz"); int found = owners.size(); Owner owner = new Owner(); owner.setFirstName("Sam"); @@ -81,19 +81,19 @@ public abstract class AbstractOwnerRepositoryTests { owner.setAddress("4, Evans Street"); owner.setCity("Wollongong"); owner.setTelephone("4444444444"); - this.ownerRepository.save(owner); - owners = this.ownerRepository.findByLastName("Schultz"); + this.clinicService.saveOwner(owner); + owners = this.clinicService.findOwnerByLastName("Schultz"); assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size()); } @Test @Transactional public void updateOwner() throws Exception { - Owner o1 = this.ownerRepository.findById(1); + Owner o1 = this.clinicService.findOwnerById(1); String old = o1.getLastName(); o1.setLastName(old + "X"); - this.ownerRepository.save(o1); - o1 = this.ownerRepository.findById(1); + this.clinicService.saveOwner(o1); + o1 = this.clinicService.findOwnerById(1); assertEquals(old + "X", o1.getLastName()); } diff --git a/src/test/java/org/springframework/samples/petclinic/repository/AbstractPetRepositoryTests.java b/src/test/java/org/springframework/samples/petclinic/repository/AbstractPetRepositoryTests.java index 691e07a..d1c715d 100644 --- a/src/test/java/org/springframework/samples/petclinic/repository/AbstractPetRepositoryTests.java +++ b/src/test/java/org/springframework/samples/petclinic/repository/AbstractPetRepositoryTests.java @@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.transaction.annotation.Transactional; @@ -42,17 +43,13 @@ import static org.junit.Assert.assertTrue; */ public abstract class AbstractPetRepositoryTests { - @Autowired - protected PetRepository petRepository; - - @Autowired - protected OwnerRepository ownerRepository; + @Autowired + protected ClinicService clinicService; @Test - @Transactional public void getPetTypes() { - Collection<PetType> petTypes = this.petRepository.findPetTypes(); + Collection<PetType> petTypes = this.clinicService.findPetTypes(); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); assertEquals("cat", petType1.getName()); @@ -61,14 +58,13 @@ public abstract class AbstractPetRepositoryTests { } @Test - @Transactional public void findPet() { - Collection<PetType> types = this.petRepository.findPetTypes(); - Pet pet7 = this.petRepository.findById(7); + Collection<PetType> types = this.clinicService.findPetTypes(); + Pet pet7 = this.clinicService.findPetById(7); assertTrue(pet7.getName().startsWith("Samantha")); assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId()); assertEquals("Jean", pet7.getOwner().getFirstName()); - Pet pet6 = this.petRepository.findById(6); + Pet pet6 = this.clinicService.findPetById(6); assertEquals("George", pet6.getName()); assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId()); assertEquals("Peter", pet6.getOwner().getFirstName()); @@ -77,30 +73,30 @@ public abstract class AbstractPetRepositoryTests { @Test @Transactional public void insertPet() { - Owner owner6 = this.ownerRepository.findById(6); + Owner owner6 = this.clinicService.findOwnerById(6); int found = owner6.getPets().size(); Pet pet = new Pet(); pet.setName("bowser"); - Collection<PetType> types = this.petRepository.findPetTypes(); + Collection<PetType> types = this.clinicService.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setBirthDate(new DateTime()); owner6.addPet(pet); assertEquals(found + 1, owner6.getPets().size()); // both storePet and storeOwner are necessary to cover all ORM tools - this.petRepository.save(pet); - this.ownerRepository.save(owner6); - owner6 = this.ownerRepository.findById(6); + this.clinicService.savePet(pet); + this.clinicService.saveOwner(owner6); + owner6 = this.clinicService.findOwnerById(6); assertEquals(found + 1, owner6.getPets().size()); } @Test @Transactional public void updatePet() throws Exception { - Pet pet7 = this.petRepository.findById(7); + Pet pet7 = this.clinicService.findPetById(7); String old = pet7.getName(); pet7.setName(old + "X"); - this.petRepository.save(pet7); - pet7 = this.petRepository.findById(7); + this.clinicService.savePet(pet7); + pet7 = this.clinicService.findPetById(7); assertEquals(old + "X", pet7.getName()); } diff --git a/src/test/java/org/springframework/samples/petclinic/repository/AbstractVetRepositoryTests.java b/src/test/java/org/springframework/samples/petclinic/repository/AbstractVetRepositoryTests.java index bd906ed..6e25f9b 100644 --- a/src/test/java/org/springframework/samples/petclinic/repository/AbstractVetRepositoryTests.java +++ b/src/test/java/org/springframework/samples/petclinic/repository/AbstractVetRepositoryTests.java @@ -15,15 +15,15 @@ */ package org.springframework.samples.petclinic.repository; +import static org.junit.Assert.assertEquals; + +import java.util.Collection; + import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; - -import static org.junit.Assert.assertEquals; /** * <p> Base class for {@link OwnerRepository} integration tests. </p> @@ -38,14 +38,13 @@ import static org.junit.Assert.assertEquals; */ public abstract class AbstractVetRepositoryTests { - @Autowired - protected VetRepository vetRepository; + @Autowired + protected ClinicService clinicService; @Test - @Transactional public void findVets() { - Collection<Vet> vets = this.vetRepository.findAll(); + Collection<Vet> vets = this.clinicService.findVets(); Vet v1 = EntityUtils.getById(vets, Vet.class, 2); assertEquals("Leary", v1.getLastName()); diff --git a/src/test/java/org/springframework/samples/petclinic/repository/AbstractVisitRepositoryTests.java b/src/test/java/org/springframework/samples/petclinic/repository/AbstractVisitRepositoryTests.java index 5960878..0d122a6 100644 --- a/src/test/java/org/springframework/samples/petclinic/repository/AbstractVisitRepositoryTests.java +++ b/src/test/java/org/springframework/samples/petclinic/repository/AbstractVisitRepositoryTests.java @@ -19,6 +19,7 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.assertEquals; @@ -36,25 +37,22 @@ import static org.junit.Assert.assertEquals; */ public abstract class AbstractVisitRepositoryTests { - @Autowired - protected VisitRepository visitRepository; - - @Autowired - protected PetRepository petRepository; + @Autowired + protected ClinicService clinicService; @Test @Transactional public void insertVisit() { - Pet pet7 = this.petRepository.findById(7); + Pet pet7 = this.clinicService.findPetById(7); int found = pet7.getVisits().size(); Visit visit = new Visit(); pet7.addVisit(visit); visit.setDescription("test"); // both storeVisit and storePet are necessary to cover all ORM tools - this.visitRepository.save(visit); - this.petRepository.save(pet7); - pet7 = this.petRepository.findById(7); + this.clinicService.saveVisit(visit); + this.clinicService.savePet(pet7); + pet7 = this.clinicService.findPetById(7); assertEquals(found + 1, pet7.getVisits().size()); } -- GitLab