mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:15:50 +00:00
migrate all JUnit Integration tests from the repo layer to the service layer (step 1)
This commit is contained in:
parent
82d6f5e870
commit
be403a3017
4 changed files with 46 additions and 53 deletions
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -27,13 +28,13 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
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>
|
* 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
|
* 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
|
* 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
|
* 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,
|
* 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
|
* 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
|
* 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 {
|
public abstract class AbstractOwnerRepositoryTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected OwnerRepository ownerRepository;
|
protected ClinicService clinicService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void findOwners() {
|
public void findOwners() {
|
||||||
Collection<Owner> owners = this.ownerRepository.findByLastName("Davis");
|
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
|
||||||
assertEquals(2, owners.size());
|
assertEquals(2, owners.size());
|
||||||
owners = this.ownerRepository.findByLastName("Daviss");
|
owners = this.clinicService.findOwnerByLastName("Daviss");
|
||||||
assertEquals(0, owners.size());
|
assertEquals(0, owners.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
|
||||||
public void findSingleOwner() {
|
public void findSingleOwner() {
|
||||||
Owner owner1 = this.ownerRepository.findById(1);
|
Owner owner1 = this.clinicService.findOwnerById(1);
|
||||||
assertTrue(owner1.getLastName().startsWith("Franklin"));
|
assertTrue(owner1.getLastName().startsWith("Franklin"));
|
||||||
Owner owner10 = this.ownerRepository.findById(10);
|
Owner owner10 = this.clinicService.findOwnerById(10);
|
||||||
assertEquals("Carlos", owner10.getFirstName());
|
assertEquals("Carlos", owner10.getFirstName());
|
||||||
|
|
||||||
assertEquals(owner1.getPets().size(), 1);
|
assertEquals(owner1.getPets().size(), 1);
|
||||||
|
@ -73,7 +73,7 @@ public abstract class AbstractOwnerRepositoryTests {
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void insertOwner() {
|
public void insertOwner() {
|
||||||
Collection<Owner> owners = this.ownerRepository.findByLastName("Schultz");
|
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Schultz");
|
||||||
int found = owners.size();
|
int found = owners.size();
|
||||||
Owner owner = new Owner();
|
Owner owner = new Owner();
|
||||||
owner.setFirstName("Sam");
|
owner.setFirstName("Sam");
|
||||||
|
@ -81,19 +81,19 @@ public abstract class AbstractOwnerRepositoryTests {
|
||||||
owner.setAddress("4, Evans Street");
|
owner.setAddress("4, Evans Street");
|
||||||
owner.setCity("Wollongong");
|
owner.setCity("Wollongong");
|
||||||
owner.setTelephone("4444444444");
|
owner.setTelephone("4444444444");
|
||||||
this.ownerRepository.save(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
owners = this.ownerRepository.findByLastName("Schultz");
|
owners = this.clinicService.findOwnerByLastName("Schultz");
|
||||||
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
|
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updateOwner() throws Exception {
|
public void updateOwner() throws Exception {
|
||||||
Owner o1 = this.ownerRepository.findById(1);
|
Owner o1 = this.clinicService.findOwnerById(1);
|
||||||
String old = o1.getLastName();
|
String old = o1.getLastName();
|
||||||
o1.setLastName(old + "X");
|
o1.setLastName(old + "X");
|
||||||
this.ownerRepository.save(o1);
|
this.clinicService.saveOwner(o1);
|
||||||
o1 = this.ownerRepository.findById(1);
|
o1 = this.clinicService.findOwnerById(1);
|
||||||
assertEquals(old + "X", o1.getLastName());
|
assertEquals(old + "X", o1.getLastName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -42,17 +43,13 @@ import static org.junit.Assert.assertTrue;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractPetRepositoryTests {
|
public abstract class AbstractPetRepositoryTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected PetRepository petRepository;
|
protected ClinicService clinicService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
protected OwnerRepository ownerRepository;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
|
||||||
public void getPetTypes() {
|
public void getPetTypes() {
|
||||||
Collection<PetType> petTypes = this.petRepository.findPetTypes();
|
Collection<PetType> petTypes = this.clinicService.findPetTypes();
|
||||||
|
|
||||||
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||||
assertEquals("cat", petType1.getName());
|
assertEquals("cat", petType1.getName());
|
||||||
|
@ -61,14 +58,13 @@ public abstract class AbstractPetRepositoryTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
|
||||||
public void findPet() {
|
public void findPet() {
|
||||||
Collection<PetType> types = this.petRepository.findPetTypes();
|
Collection<PetType> types = this.clinicService.findPetTypes();
|
||||||
Pet pet7 = this.petRepository.findById(7);
|
Pet pet7 = this.clinicService.findPetById(7);
|
||||||
assertTrue(pet7.getName().startsWith("Samantha"));
|
assertTrue(pet7.getName().startsWith("Samantha"));
|
||||||
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId());
|
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId());
|
||||||
assertEquals("Jean", pet7.getOwner().getFirstName());
|
assertEquals("Jean", pet7.getOwner().getFirstName());
|
||||||
Pet pet6 = this.petRepository.findById(6);
|
Pet pet6 = this.clinicService.findPetById(6);
|
||||||
assertEquals("George", pet6.getName());
|
assertEquals("George", pet6.getName());
|
||||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId());
|
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId());
|
||||||
assertEquals("Peter", pet6.getOwner().getFirstName());
|
assertEquals("Peter", pet6.getOwner().getFirstName());
|
||||||
|
@ -77,30 +73,30 @@ public abstract class AbstractPetRepositoryTests {
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void insertPet() {
|
public void insertPet() {
|
||||||
Owner owner6 = this.ownerRepository.findById(6);
|
Owner owner6 = this.clinicService.findOwnerById(6);
|
||||||
int found = owner6.getPets().size();
|
int found = owner6.getPets().size();
|
||||||
Pet pet = new Pet();
|
Pet pet = new Pet();
|
||||||
pet.setName("bowser");
|
pet.setName("bowser");
|
||||||
Collection<PetType> types = this.petRepository.findPetTypes();
|
Collection<PetType> types = this.clinicService.findPetTypes();
|
||||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||||
pet.setBirthDate(new DateTime());
|
pet.setBirthDate(new DateTime());
|
||||||
owner6.addPet(pet);
|
owner6.addPet(pet);
|
||||||
assertEquals(found + 1, owner6.getPets().size());
|
assertEquals(found + 1, owner6.getPets().size());
|
||||||
// both storePet and storeOwner are necessary to cover all ORM tools
|
// both storePet and storeOwner are necessary to cover all ORM tools
|
||||||
this.petRepository.save(pet);
|
this.clinicService.savePet(pet);
|
||||||
this.ownerRepository.save(owner6);
|
this.clinicService.saveOwner(owner6);
|
||||||
owner6 = this.ownerRepository.findById(6);
|
owner6 = this.clinicService.findOwnerById(6);
|
||||||
assertEquals(found + 1, owner6.getPets().size());
|
assertEquals(found + 1, owner6.getPets().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updatePet() throws Exception {
|
public void updatePet() throws Exception {
|
||||||
Pet pet7 = this.petRepository.findById(7);
|
Pet pet7 = this.clinicService.findPetById(7);
|
||||||
String old = pet7.getName();
|
String old = pet7.getName();
|
||||||
pet7.setName(old + "X");
|
pet7.setName(old + "X");
|
||||||
this.petRepository.save(pet7);
|
this.clinicService.savePet(pet7);
|
||||||
pet7 = this.petRepository.findById(7);
|
pet7 = this.clinicService.findPetById(7);
|
||||||
assertEquals(old + "X", pet7.getName());
|
assertEquals(old + "X", pet7.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,15 +15,15 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.repository;
|
package org.springframework.samples.petclinic.repository;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
|
||||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p> Base class for {@link OwnerRepository} integration tests. </p>
|
* <p> Base class for {@link OwnerRepository} integration tests. </p>
|
||||||
|
@ -38,14 +38,13 @@ import static org.junit.Assert.assertEquals;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractVetRepositoryTests {
|
public abstract class AbstractVetRepositoryTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected VetRepository vetRepository;
|
protected ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
|
||||||
public void findVets() {
|
public void findVets() {
|
||||||
Collection<Vet> vets = this.vetRepository.findAll();
|
Collection<Vet> vets = this.clinicService.findVets();
|
||||||
|
|
||||||
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
||||||
assertEquals("Leary", v1.getLastName());
|
assertEquals("Leary", v1.getLastName());
|
||||||
|
|
|
@ -19,6 +19,7 @@ import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -36,25 +37,22 @@ import static org.junit.Assert.assertEquals;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractVisitRepositoryTests {
|
public abstract class AbstractVisitRepositoryTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected VisitRepository visitRepository;
|
protected ClinicService clinicService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
protected PetRepository petRepository;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void insertVisit() {
|
public void insertVisit() {
|
||||||
Pet pet7 = this.petRepository.findById(7);
|
Pet pet7 = this.clinicService.findPetById(7);
|
||||||
int found = pet7.getVisits().size();
|
int found = pet7.getVisits().size();
|
||||||
Visit visit = new Visit();
|
Visit visit = new Visit();
|
||||||
pet7.addVisit(visit);
|
pet7.addVisit(visit);
|
||||||
visit.setDescription("test");
|
visit.setDescription("test");
|
||||||
// both storeVisit and storePet are necessary to cover all ORM tools
|
// both storeVisit and storePet are necessary to cover all ORM tools
|
||||||
this.visitRepository.save(visit);
|
this.clinicService.saveVisit(visit);
|
||||||
this.petRepository.save(pet7);
|
this.clinicService.savePet(pet7);
|
||||||
pet7 = this.petRepository.findById(7);
|
pet7 = this.clinicService.findPetById(7);
|
||||||
assertEquals(found + 1, pet7.getVisits().size());
|
assertEquals(found + 1, pet7.getVisits().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue