From 80269539e27062592f4c28b0d5498f38fbc2d578 Mon Sep 17 00:00:00 2001 From: Dave Syer <dsyer@pivotal.io> Date: Sun, 8 Jan 2017 15:45:20 +0000 Subject: [PATCH] Configure caching properly to avoid error in vets --- .../samples/petclinic/vet/Specialty.java | 4 ++- .../samples/petclinic/vet/Vet.java | 9 ++--- src/main/resources/application.properties | 1 + .../system/CrashControllerTests.java | 34 ++++++------------- .../system/ProductionConfigurationTests.java | 23 +++++++++++++ 5 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java diff --git a/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java b/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java index 826e04a..5691c24 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java @@ -15,6 +15,8 @@ */ package org.springframework.samples.petclinic.vet; +import java.io.Serializable; + import javax.persistence.Entity; import javax.persistence.Table; @@ -27,6 +29,6 @@ import org.springframework.samples.petclinic.model.NamedEntity; */ @Entity @Table(name = "specialties") -public class Specialty extends NamedEntity { +public class Specialty extends NamedEntity implements Serializable { } diff --git a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java index 8294237..3cde3d1 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.vet; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -43,11 +44,10 @@ import org.springframework.samples.petclinic.model.Person; */ @Entity @Table(name = "vets") -public class Vet extends Person { +public class Vet extends Person implements Serializable { @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), - inverseJoinColumns = @JoinColumn(name = "specialty_id")) + @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set<Specialty> specialties; protected Set<Specialty> getSpecialtiesInternal() { @@ -64,7 +64,8 @@ public class Vet extends Person { @XmlElement public List<Specialty> getSpecialties() { List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal()); - PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true)); + PropertyComparator.sort(sortedSpecs, + new MutableSortDefinition("name", true, true)); return Collections.unmodifiableList(sortedSpecs); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3562d7d..cd1aea2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,3 +22,4 @@ logging.level.org.springframework=INFO # Active Spring profiles spring.profiles.active=production +spring.cache.cache-names=vets \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java index 1617692..3f108bf 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -1,20 +1,19 @@ package org.springframework.samples.petclinic.system; -import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; + 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.system.CrashController; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; /** * Test class for {@link CrashController} @@ -22,31 +21,18 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Colin But */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = PetClinicApplication.class) -@WebAppConfiguration // Waiting https://github.com/spring-projects/spring-boot/issues/5574 @Ignore +@WebMvcTest(controllers = CrashController.class) public class CrashControllerTests { @Autowired - private CrashController crashController; - private MockMvc mockMvc; - @Before - public void setup() { - this.mockMvc = MockMvcBuilders - .standaloneSetup(crashController) - //.setHandlerExceptionResolvers(new SimpleMappingExceptionResolver()) - .build(); - } - @Test public void testTriggerException() throws Exception { - mockMvc.perform(get("/oups")) - .andExpect(view().name("exception")) - .andExpect(model().attributeExists("exception")) - .andExpect(forwardedUrl("exception")) - .andExpect(status().isOk()); + mockMvc.perform(get("/oups")).andExpect(view().name("exception")) + .andExpect(model().attributeExists("exception")) + .andExpect(forwardedUrl("exception")).andExpect(status().isOk()); } } diff --git a/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java new file mode 100644 index 0000000..9636e36 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java @@ -0,0 +1,23 @@ +package org.springframework.samples.petclinic.system; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.samples.petclinic.vet.VetRepository; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ProductionConfigurationTests { + + @Autowired + private VetRepository vets; + + @Test + public void testFindAll() throws Exception { + vets.findAll(); + vets.findAll(); // served from cache + } +} -- GitLab