mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 04:45:49 +00:00
Fixed test using Spring MVC Test framework
This commit is contained in:
parent
222f1d1b30
commit
70c046a927
6 changed files with 38 additions and 115 deletions
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
<logger name="org.springframework.samples.petclinic" level="debug"/>
|
<logger name="org.springframework.samples.petclinic" level="debug"/>
|
||||||
|
|
||||||
|
<logger name="org.springframework.web.servlet.view" level="debug"/>
|
||||||
|
|
||||||
<root level="info">
|
<root level="info">
|
||||||
<appender-ref ref="console"/>
|
<appender-ref ref="console"/>
|
||||||
</root>
|
</root>
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
<!-- uses WebJars so Javascript and CSS libs can be declared as Maven dependencies (Bootstrap, jQuery...) -->
|
<!-- uses WebJars so Javascript and CSS libs can be declared as Maven dependencies (Bootstrap, jQuery...) -->
|
||||||
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
|
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
|
||||||
|
|
||||||
<mvc:view-controller path="/" view-name="welcome"/>
|
<mvc:view-controller path="/" view-name="welcome" />
|
||||||
|
|
||||||
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
|
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
|
||||||
<property name="formatters">
|
<property name="formatters">
|
||||||
|
|
|
@ -17,14 +17,9 @@
|
||||||
-->
|
-->
|
||||||
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
|
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
|
||||||
<property name="contentNegotiationManager" ref="cnManager"/>
|
<property name="contentNegotiationManager" ref="cnManager"/>
|
||||||
</bean>
|
|
||||||
|
|
||||||
<!-- Simple strategy: only path extension is taken into account -->
|
|
||||||
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
|
|
||||||
<property name="favorPathExtension" value="true"/>
|
|
||||||
<property name="ignoreAcceptHeader" value="true"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
|
<property name="viewResolvers">
|
||||||
|
<list>
|
||||||
<!-- Default viewClass: JSTL view (JSP with html output) -->
|
<!-- Default viewClass: JSTL view (JSP with html output) -->
|
||||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||||
<!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
|
<!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
|
||||||
|
@ -34,6 +29,24 @@
|
||||||
|
|
||||||
<!-- Used here for 'xml' and 'atom' views -->
|
<!-- Used here for 'xml' and 'atom' views -->
|
||||||
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
|
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
|
||||||
|
</list>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<!-- Simple strategy: only path extension is taken into account -->
|
||||||
|
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
|
||||||
|
<property name="favorPathExtension" value="true"/>
|
||||||
|
<property name="ignoreAcceptHeader" value="true"/>
|
||||||
|
<property name="defaultContentType" value="text/html"/>
|
||||||
|
|
||||||
|
<property name="mediaTypes">
|
||||||
|
<map>
|
||||||
|
<entry key="html" value="text/html" />
|
||||||
|
<entry key="xml" value="application/xml" />
|
||||||
|
<entry key="atom" value="application/atom+xml" />
|
||||||
|
</map>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<!-- Renders an Atom feed of the visits. Used by the BeanNameViewResolver -->
|
<!-- Renders an Atom feed of the visits. Used by the BeanNameViewResolver -->
|
||||||
<bean id="vets/vetList.atom" class="org.springframework.samples.petclinic.web.VetsAtomView"/>
|
<bean id="vets/vetList.atom" class="org.springframework.samples.petclinic.web.VetsAtomView"/>
|
||||||
|
|
|
@ -1,92 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2009 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.web;
|
|
||||||
|
|
||||||
import com.sun.syndication.feed.atom.Entry;
|
|
||||||
import com.sun.syndication.feed.atom.Feed;
|
|
||||||
import org.joda.time.DateTime;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
|
||||||
import org.springframework.samples.petclinic.model.Visit;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
* @author Michael Isvy
|
|
||||||
*/
|
|
||||||
public class VisitsAtomViewTest {
|
|
||||||
|
|
||||||
private VetsAtomView visitView;
|
|
||||||
|
|
||||||
private Map<String, Object> model;
|
|
||||||
|
|
||||||
private Feed feed;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
visitView = new VetsAtomView();
|
|
||||||
PetType dog = new PetType();
|
|
||||||
dog.setName("dog");
|
|
||||||
Pet bello = new Pet();
|
|
||||||
bello.setName("Bello");
|
|
||||||
bello.setType(dog);
|
|
||||||
Visit belloVisit = new Visit();
|
|
||||||
belloVisit.setPet(bello);
|
|
||||||
belloVisit.setDate(new DateTime(2009, 1, 1, 1, 1));
|
|
||||||
belloVisit.setDescription("Bello visit");
|
|
||||||
Pet wodan = new Pet();
|
|
||||||
wodan.setName("Wodan");
|
|
||||||
wodan.setType(dog);
|
|
||||||
Visit wodanVisit = new Visit();
|
|
||||||
wodanVisit.setPet(wodan);
|
|
||||||
wodanVisit.setDate(new DateTime(2009, 1, 2, 1, 1));
|
|
||||||
wodanVisit.setDescription("Wodan visit");
|
|
||||||
List<Visit> visits = new ArrayList<Visit>();
|
|
||||||
visits.add(belloVisit);
|
|
||||||
visits.add(wodanVisit);
|
|
||||||
|
|
||||||
model = new HashMap<String, Object>();
|
|
||||||
model.put("visits", visits);
|
|
||||||
feed = new Feed();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void buildFeedMetadata() {
|
|
||||||
visitView.buildFeedMetadata(model, feed, null);
|
|
||||||
|
|
||||||
assertNotNull("No id set", feed.getId());
|
|
||||||
assertNotNull("No title set", feed.getTitle());
|
|
||||||
assertEquals("Invalid update set", new DateTime(2009, 1, 2, 1, 1).toDate(), feed.getUpdated());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void buildFeedEntries() throws Exception {
|
|
||||||
List<Entry> entries = visitView.buildFeedEntries(model, null, null);
|
|
||||||
assertEquals("Invalid amount of entries", 2, entries.size());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -31,8 +31,11 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
@ -40,11 +43,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
// Spring configuration files that are inside WEB-INF folder can be referenced here because they've been
|
@ContextConfiguration("VisitsViewTest-config.xml")
|
||||||
// added to the classpath inside the Maven pom.xml file (inside <build> <testResources> ... </testResources> </build>)
|
|
||||||
@ContextConfiguration("VisitsAtomViewTestWithContainer-config.xml")
|
|
||||||
@ActiveProfiles("jdbc")
|
@ActiveProfiles("jdbc")
|
||||||
public class VisitsAtomViewWithContainerTest {
|
public class VisitsViewTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webApplicationContext;
|
private WebApplicationContext webApplicationContext;
|
||||||
|
@ -57,13 +58,12 @@ public class VisitsAtomViewWithContainerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getVisits() throws Exception {
|
public void getVisitsXml() throws Exception {
|
||||||
//gDickens: MediaType is not used
|
ResultActions actions = this.mockMvc.perform(get("/vets.xml").accept(MediaType.APPLICATION_XML));
|
||||||
MediaType mediaType = MediaType.APPLICATION_ATOM_XML;
|
actions.andDo(print()); // action is logged into the console
|
||||||
ResultActions actions = this.mockMvc.perform(get("/vets.atom"));
|
|
||||||
actions.andExpect(status().isOk());
|
actions.andExpect(status().isOk());
|
||||||
actions.andExpect(xpath("//*").string(containsString("Pet ClinicService Visits")));
|
actions.andExpect(content().contentType("application/xml"));
|
||||||
actions.andExpect(content().contentType("application/atom+xml"));
|
actions.andExpect(xpath("/vets/vetList[id=1]/firstName").string(containsString("James")));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue