adding config files (#554)
* adding config files * adding test class * adding autowired annotation
This commit is contained in:
parent
67f24c25af
commit
96f4f732f8
@ -0,0 +1,37 @@
|
|||||||
|
package org.baeldung.controller.config;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
public class StudentControllerConfig implements WebApplicationInitializer {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartup(ServletContext sc) throws ServletException {
|
||||||
|
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||||
|
root.register(WebConfig.class);
|
||||||
|
|
||||||
|
root.refresh();
|
||||||
|
root.setServletContext(sc);
|
||||||
|
|
||||||
|
// Manages the lifecycle of the root application context
|
||||||
|
sc.addListener(new ContextLoaderListener(root));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
DispatcherServlet dv =new DispatcherServlet(new GenericWebApplicationContext());
|
||||||
|
|
||||||
|
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc",dv );
|
||||||
|
appServlet.setLoadOnStartup(1);
|
||||||
|
appServlet.addMapping("/test/*");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package org.baeldung.controller.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan(basePackages= {"org.baledung.controller.controller","org.baledung.controller.config" })
|
||||||
|
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
|
@Override
|
||||||
|
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||||
|
configurer.enable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
|
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||||
|
bean.setPrefix("/WEB-INF/");
|
||||||
|
bean.setSuffix(".jsp");
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package org.baeldung.controller;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.baeldung.controller.config.WebConfig;
|
||||||
|
import org.baeldung.controller.student.Student;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes={WebConfig.class},loader=AnnotationConfigContextLoader.class )
|
||||||
|
public class ControllerAnnotationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ServletContext servletContext;
|
||||||
|
|
||||||
|
private Student selectedStudent;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||||
|
|
||||||
|
selectedStudent = new Student();
|
||||||
|
selectedStudent.setId(1);
|
||||||
|
selectedStudent.setName("Peter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTestController() throws Exception {
|
||||||
|
|
||||||
|
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/"))
|
||||||
|
.andReturn()
|
||||||
|
.getModelAndView();
|
||||||
|
|
||||||
|
// validate modal data
|
||||||
|
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||||
|
|
||||||
|
// validate view name
|
||||||
|
Assert.assertSame(mv.getViewName(), "welcome");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRestController() throws Exception {
|
||||||
|
|
||||||
|
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1))
|
||||||
|
.andReturn().getResponse()
|
||||||
|
.getContentAsString();
|
||||||
|
|
||||||
|
ObjectMapper reader = new ObjectMapper();
|
||||||
|
|
||||||
|
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||||
|
|
||||||
|
Assert.assertEquals(selectedStudent, studentDetails);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRestAnnotatedController() throws Exception {
|
||||||
|
|
||||||
|
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1))
|
||||||
|
.andReturn().getResponse()
|
||||||
|
.getContentAsString();
|
||||||
|
|
||||||
|
ObjectMapper reader = new ObjectMapper();
|
||||||
|
|
||||||
|
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||||
|
|
||||||
|
Assert.assertEquals(selectedStudent, studentDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user