Announcement Announcement Module
Collapse
No announcement yet.
Two simple integration testing design questions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Two simple integration testing design questions

    Hi,

    I'm trying to code some integration testing against a controller on Spring 3.2 fronting a Spring Data repository on a MySQL database.

    I have two questions:

    Should I use the mock mvc (@WebAppConfiguration and its this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplica tionContext).build() or should I use the rest template (@Autowired*private RestTemplate restTemplate) ?

    Does the rest template use a mock http server or does it need the explicit config and startup of a Jetty server ?

    Thanks for any directions.

    Kind Regards,
    Last edited by stephaneeybert; May 24th, 2013, 09:12 AM.

  • #2
    I now understand that the mock mvc acts on the server side and the rest template on the client side, so there are not playing on the same field, and are not an alternative to each other.

    Comment


    • #3
      When running the following test:
      Code:
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = { "classpath:test.xml", "classpath:jpa.xml", "classpath:data-source.xml", "classpath:log4j.xml" })
      @WebAppConfiguration
      public abstract class AbstractControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
      
      	@Autowired
          private WebApplicationContext webApplicationContext;
          
      	protected MockMvc mockMvc;
      	
          @Before
      	public void setUpTestDataWithinTransaction() {	
      	}
      	
          @Before
          public void setup() {
              this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
          }
          
      }
      
      public class AdminControllerTest extends AbstractControllerTest {
      
      	@Autowired 
      	AdminRepository adminRepository;
      
      	private Admin admin0;
      	
          @Before
          public void beforeAnyTest() throws Exception {
              admin0 = new Admin();
              admin0.setLogin("stephane");
              admin0.setPassword("toto");
              admin0.setPasswordSalt("");
              admin0.setEmail("[email protected]");
              admin0.setFirstname("Stephane");
              admin0.setLastname("Eybert");
      
              admin0 = adminRepository.save(admin0);
          }
      
          @Test
          public void testHello() throws Exception {
              this.mockMvc.perform(get("/admin/hello")).andExpect(status().isOk());
          }
      I get a failure and it says:

      <<< FAILURE! java.lang.AssertionError: Status expected:<200> but was:<404>

      Comment


      • #4
        Here is my web.xml file content:
        Code:
        	<display-name>Spring Web Demo</display-name>
        	
        	<description>A spring web demo</description>
        
        	<listener>
        		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        	</listener>
        
        	<context-param>
            	<param-name>contextConfigLocation</param-name>
            	<param-value>classpath:jpa.xml, classpath:data-source.xml, classpath:service-jpa.xml, classpath:log4j.xml</param-value>
        	</context-param>
        
        	<welcome-file-list>
        		<welcome-file>index.jsp</welcome-file>
        	</welcome-file-list>
            
            <session-config>
                <session-timeout>30</session-timeout>
            </session-config>
        
            <error-page>
                <error-code>404</error-code>
                <location>/html/404.html</location>
            </error-page>
        	
        	<servlet>
        		<servlet-name>learnintouch</servlet-name>
        		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        		<load-on-startup>1</load-on-startup>
        	</servlet>
         
        	<servlet-mapping>
        		<servlet-name>learnintouch</servlet-name>
        		<url-pattern>/</url-pattern>
        	</servlet-mapping>
        And here is the learnintouch-servlet.xml file content:
        Code:
        	<context:component-scan base-package="com.thalasoft.learnintouch.web.controller" />
        
        	<mvc:annotation-driven>
        		<mvc:argument-resolvers>
        			<bean class="org.springframework.data.web.PageableArgumentResolver"></bean>
        		</mvc:argument-resolvers>
        	</mvc:annotation-driven>
        
        	<bean id="viewResolver"
        		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        		<property name="viewClass"
        			value="org.springframework.web.servlet.view.JstlView" />
        		<property name="prefix" value="/WEB-INF/jsp/" />
        		<property name="suffix" value=".jsp" />
        	</bean>
        I thought using the annotation @WebAppConfiguration in my test class would load both of these two above files, but it appears it was not the case.

        After adding the following location file:src/main/webapp/WEB-INF/learnintouch-servlet.xml to the annotation @ContextConfiguration the test finally worked fine.

        Here is the annotated test:

        Code:
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/learnintouch-servlet.xml", "classpath:test.xml", "classpath:service-jpa.xml", "classpath:jpa.xml", "classpath:data-source.xml", "classpath:log4j.xml" })
        @WebAppConfiguration
        public abstract class AbstractControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
        All of this was guess work to me.

        I wonder if this is the right way to load the context in an integration test.

        Also, more explaining on what is to be loaded and how to have it loaded would be welcomed in the Spring reference documentation.

        Comment

        Working...
        X