好久沒用過單元測試了,主要是和我現在做的事情不搭邊,看來又要重拾飯碗了,哈哈。
啥也不說,先貼代碼:
一:測試Controller相關
1 import java.util.List; 2 3 import org.junit.runner.RunWith; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.test.context.ContextConfiguration; 6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 import org.springframework.test.context.transaction.TransactionConfiguration; 8 import org.springframework.test.context.web.WebAppConfiguration; 9 import org.springframework.test.web.servlet.MockMvc; 10 import org.springframework.test.web.servlet.MvcResult; 11 import org.springframework.test.web.servlet.ResultActions; 12 import org.springframework.test.web.servlet.result.MockMvcResultHandlers; 13 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 14 15 import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; 16 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 18 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 19 import org.springframework.transaction.annotation.Transactional; 20 import org.springframework.web.context.WebApplicationContext; 21 import org.junit.Assert; 22 import org.junit.Before; 23 import org.junit.Test; 24 import org.martin.cluster.beans.CurrentUser; 25 import org.martin.member.domain.S2mOrgMember; 26 27 @RunWith(SpringJUnit4ClassRunner.class) 28 @WebAppConfiguration(value="src/main/webapp") 29 @ContextConfiguration(locations={"classpath*:/ApplicationContext.xml","classpath*:/s2mCommon-servlet.xml"}) 30 @TransactionConfiguration(defaultRollback = true) 31 @Transactional 32 public class ControllerTest { 33 @Autowired 34 private WebApplicationContext webApplicationContext; 35 36 private MockMvc mockMvc ; 37 38 @Before 39 public void setup() { 40 //注意该方法来自类:MockMvcBuilders 41 this.mockMvc = webAppContextSetup(this.webApplicationContext).build(); 42 } 43 44 @Test 45 public void t1() throws Exception{ 46 //get方法来之MockMvcRequestBuilders 47 MvcResult rs = mockMvc.perform(post("/memberController/to/?page=index")) 48 .andExpect(MockMvcResultMatchers.view().name("index")) 49 .andExpect(MockMvcResultMatchers.model().attributeExists("page")) 50 .andDo(MockMvcResultHandlers.print()).andExpect(status().isOk()) 51 .andReturn(); 52 53 Assert.assertNotNull(rs.getModelAndView().getViewName()); 54 } 55 }
二、測試Manager/Dao
import java.util.List; import javax.servlet.SessionCookieConfig; 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.transaction.TransactionConfiguration; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; 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.MockMvcResultHandlers.*; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.martin.cluster.beans.CurrentUser; import org.martin.member.domain.S2mOrgMember; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration(value="src/main/webapp") @ContextConfiguration(locations={"classpath*:/ApplicationContext.xml"}) @TransactionConfiguration(defaultRollback = true) @Transactional public class ControllerTest { @Autowired private MemberManager memberManager; @Test public void t1() throws Exception{ //測試內容 } }
認真看了代碼的,應該發現沒啥不同,就是在配置ContextConfiguration的時候傳入參數不同
下面一一解釋:
@RunWith(SpringJUnit4ClassRunner.class) 表示此測試是運行于spring環境
@WebAppConfiguration(value="src/main/webapp") 表示這是web应用测试,value是web項目的根目錄
@ContextConfiguration web應用配置文件,如spring的配置文件,和*-servlet.xml文件等(注:如果需要測試Controller必須配置*-servlet.xml配置文件)
@TransactionConfiguration(defaultRollback = true) 事物參數配置,defaultRollback默認自動回滾
@Transactional 配置事物
@Rollback(false) 額外:可以單獨配置在方法上,可以不回滾
--------------------------------------------------------------------------------------------------------------------------------------------------------