###前提・実現したいこと
springを使用して、DBに登録されているデータを一覧表示させるプログラムを組みました。
そのプログラムをJUnitを使用してテストし、カバレッジを通したいのですが、どのようにプログラムを書けばいいのかわからずに困っています。
###HomeController.java
package jp.org.web; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping(value = "/", method = RequestMethod.GET) public String index(Model model) { List<Map<String, Object>> tbl = jdbcTemplate.queryForList("SELECT name,number,mail FROM data_table"); model.addAttribute("tbl", tbl); return "index"; } }
###HomeControllerTest.java
package jp.org.web; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; 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.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/ApplicationContext.xml" }) @WebAppConfiguration public class HomeControllerTest { @Autowired WebApplicationContext context; MockMvc mockMvc; @InjectMocks HomeController controller; @Before public void setupMockMvc() { this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testTask8Search_view() throws Exception { mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/WEB-INF/index.jsp")); } }
###ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <annotation-driven /> <resources mapping="/resources/**" location="/resources/" /> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="jp.org.web" /> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> <beans:property name="url" value="jdbc:mysql://localhost/テーブル名" /> <beans:property name="username" value="root" /> <beans:property name="password" value="" /> </beans:bean> <context:property-placeholder location="classpath:jdbc.properties" /> <beans:bean id="appProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <beans:property name="ignoreResourceNotFound" value="true"/> <beans:property name="locations" value="classpath:app.properties"/> </beans:bean> <context:property-placeholder properties-ref="appProps" /> <beans:bean class="org.springframework.jdbc.core.JdbcTemplate"> <beans:constructor-arg ref="dataSource" /> </beans:bean> </beans:beans>
みなさま何卒よろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。