Skip to content
Snippets Groups Projects
Commit 90f4c1ac authored by Maciej Szarlinski's avatar Maciej Szarlinski
Browse files

Activate and fix AbstractClinicServiceTests

parent 536e7e9a
No related branches found
No related tags found
No related merge requests found
...@@ -50,7 +50,7 @@ public class Owner extends Person { ...@@ -50,7 +50,7 @@ public class Owner extends Person {
@Digits(fraction = 0, integer = 10) @Digits(fraction = 0, integer = 10)
private String telephone; private String telephone;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "owner")
private Set<Pet> pets; private Set<Pet> pets;
......
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
package org.springframework.samples.petclinic.domain.model; package org.springframework.samples.petclinic.domain.model;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.samples.petclinic.PetClinicApplication;
import org.springframework.samples.petclinic.domain.model.owner.Owner; import org.springframework.samples.petclinic.domain.model.owner.Owner;
import org.springframework.samples.petclinic.domain.model.owner.OwnerService; import org.springframework.samples.petclinic.domain.model.owner.OwnerService;
import org.springframework.samples.petclinic.domain.model.pet.Pet; import org.springframework.samples.petclinic.domain.model.pet.Pet;
...@@ -26,7 +29,8 @@ import org.springframework.samples.petclinic.domain.model.vet.Vet; ...@@ -26,7 +29,8 @@ import org.springframework.samples.petclinic.domain.model.vet.Vet;
import org.springframework.samples.petclinic.domain.model.vet.VetService; import org.springframework.samples.petclinic.domain.model.vet.VetService;
import org.springframework.samples.petclinic.domain.model.visit.Visit; import org.springframework.samples.petclinic.domain.model.visit.Visit;
import org.springframework.samples.petclinic.domain.model.visit.VisitService; import org.springframework.samples.petclinic.domain.model.visit.VisitService;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Collection; import java.util.Collection;
...@@ -34,25 +38,10 @@ import java.util.Date; ...@@ -34,25 +38,10 @@ import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** @RunWith(SpringJUnit4ClassRunner.class)
* <p> Base class for {@link ClinicService} integration tests. </p> <p> Subclasses should specify Spring context @SpringBootTest(classes = PetClinicApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
* configuration using {@link ContextConfiguration @ContextConfiguration} annotation </p> <p> @ActiveProfiles("test")
* AbstractclinicServiceTests and its subclasses benefit from the following services provided by the Spring public class PetClinicTests {
* 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></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
* ApplicationContext} is also inherited and can be used for explicit bean lookup if necessary. </li> </ul>
*
* @author Ken Krebs
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
public abstract class AbstractClinicServiceTests {
@Autowired @Autowired
protected OwnerService ownerService; protected OwnerService ownerService;
...@@ -100,7 +89,7 @@ public abstract class AbstractClinicServiceTests { ...@@ -100,7 +89,7 @@ public abstract class AbstractClinicServiceTests {
@Test @Test
@Transactional @Transactional
public void shouldUpdateOwner() { public void shouldUpdateOwner() {
Owner owner = this.ownerService.findOwnerById(1); Owner owner = this.ownerService.findOwnerById(1);
String oldLastName = owner.getLastName(); String oldLastName = owner.getLastName();
String newLastName = oldLastName + "X"; String newLastName = oldLastName + "X";
...@@ -113,87 +102,87 @@ public abstract class AbstractClinicServiceTests { ...@@ -113,87 +102,87 @@ public abstract class AbstractClinicServiceTests {
assertThat(owner.getLastName()).isEqualTo(newLastName); assertThat(owner.getLastName()).isEqualTo(newLastName);
} }
@Test @Test
public void shouldFindPetWithCorrectId() { public void shouldFindPetWithCorrectId() {
Pet pet7 = this.petService.findPetById(7); Pet pet7 = this.petService.findPetById(7);
assertThat(pet7.getName()).startsWith("Samantha"); assertThat(pet7.getName()).startsWith("Samantha");
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
} }
@Test @Test
public void shouldFindAllPetTypes() { public void shouldFindAllPetTypes() {
Collection<PetType> petTypes = this.petService.findPetTypes(); Collection<PetType> petTypes = this.petService.findPetTypes();
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertThat(petType1.getName()).isEqualTo("cat"); assertThat(petType1.getName()).isEqualTo("cat");
PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4); PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
assertThat(petType4.getName()).isEqualTo("snake"); assertThat(petType4.getName()).isEqualTo("snake");
} }
@Test @Test
@Transactional @Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() { public void shouldInsertPetIntoDatabaseAndGenerateId() {
Owner owner6 = this.ownerService.findOwnerById(6); Owner owner6 = this.ownerService.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.petService.findPetTypes(); Collection<PetType> types = this.petService.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date()); pet.setBirthDate(new Date());
owner6.addPet(pet); owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1); assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.petService.savePet(pet); this.petService.savePet(pet);
this.ownerService.saveOwner(owner6); this.ownerService.saveOwner(owner6);
owner6 = this.ownerService.findOwnerById(6); owner6 = this.ownerService.findOwnerById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1); assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated // checks that id has been generated
assertThat(pet.getId()).isNotNull(); assertThat(pet.getId()).isNotNull();
} }
@Test @Test
@Transactional @Transactional
public void shouldUpdatePetName() throws Exception { public void shouldUpdatePetName() throws Exception {
Pet pet7 = this.petService.findPetById(7); Pet pet7 = this.petService.findPetById(7);
String oldName = pet7.getName(); String oldName = pet7.getName();
String newName = oldName + "X"; String newName = oldName + "X";
pet7.setName(newName); pet7.setName(newName);
this.petService.savePet(pet7); this.petService.savePet(pet7);
pet7 = this.petService.findPetById(7); pet7 = this.petService.findPetById(7);
assertThat(pet7.getName()).isEqualTo(newName); assertThat(pet7.getName()).isEqualTo(newName);
} }
@Test @Test
public void shouldFindVets() { public void shouldFindVets() {
Collection<Vet> vets = this.vetService.findVets(); Collection<Vet> vets = this.vetService.findVets();
Vet vet = EntityUtils.getById(vets, Vet.class, 3); Vet vet = EntityUtils.getById(vets, Vet.class, 3);
assertThat(vet.getLastName()).isEqualTo("Douglas"); assertThat(vet.getLastName()).isEqualTo("Douglas");
assertThat(vet.getNrOfSpecialties()).isEqualTo(2); assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry"); assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery"); assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
} }
@Test @Test
@Transactional @Transactional
public void shouldAddNewVisitForPet() { public void shouldAddNewVisitForPet() {
Pet pet7 = this.petService.findPetById(7); Pet pet7 = this.petService.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");
this.visitService.saveVisit(visit); this.visitService.saveVisit(visit);
this.petService.savePet(pet7); this.petService.savePet(pet7);
pet7 = this.petService.findPetById(7); pet7 = this.petService.findPetById(7);
assertThat(pet7.getVisits().size()).isEqualTo(found + 1); assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
assertThat(visit.getId()).isNotNull(); assertThat(visit.getId()).isNotNull();
} }
} }
petclinic.database=hsqldb
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment