1. 程式人生 > >spring MVC中controller層和service層的junit4測試

spring MVC中controller層和service層的junit4測試

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet
.setup.MockMvcBuilders.*; import javax.annotation.Resource; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.http.MediaType; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit
4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; import sz.model.api.EmailSendApiInput; import com.mars.fw.util.json.JsonUtil; @RunWith(SpringJUnit4ClassRunner.class
) @WebAppConfiguration @ContextConfiguration(locations = { "classpath:config/spring-mvc.xml", "classpath:config/spring-resources.xml", "classpath:config/spring-application.xml", "classpath:config/spring-security.xml" }) public class EmailSendApiControllerTest { @Resource WebApplicationContext wac; MockMvc mockMvc; @Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).build(); } @Test public void emailSendTest2() throws Exception { //為引數做些測試資料 EmailSendApiInput input = new EmailSendApiInput(); input.setUserId("123"); input.setPassword("123"); //轉成json String json = JsonUtil.toJson(input); mockMvc.perform( (post("/email/send") .contentType(MediaType.APPLICATION_JSON).content(json))) .andExpect(status().isOk()).andDo(print()); } }

相關推薦

spring MVCcontrollerservicejunit4測試

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResul

controllerservice的作用

1.在controller和service裡都寫那些程式碼?                   Controller,從字面上

Spring配置事務在DaoService

【轉載自http://www.blogjava.net/hk2000c/archive/2007/11/01/157508.html】 【感謝原作者:hk2000c】 Spring通過AOP實現宣告式事務管理。通常通過TransactionProxyFactoryBean設

spring quartzy的spring注入問題(daoservice

許久沒有寫博文了,今天來寫寫,畢業設計中遇到的某個小問題,及解決方法。 畢業設計中涉及到spring的定時器quartzy的使用, 遇到的問題是:quartzy中不能使用spring注入的方式注入dao層和service層,原因是spring在載入的時候先載入quarzy,後加載bean

javadaoservice的區別,為什麼要用service

讀了下面的文章 讓我豁然開朗 轉文: 首先解釋面上意思,service是業務層,dao是資料訪問層。 呵呵,這個問題我曾經也有過,記得以前剛學程式設計的時候,都是在service裡直接呼叫dao,service裡面就new一個dao類物件,呼叫,其他有意義的事沒

[SSM]springmvc daoservice的區別

首先解釋面上意思,service是業務層,dao是資料訪問層。 呵呵,這個問題我曾經也有過,記得以前剛學程式設計的時候,都是在service裡直接呼叫dao,service裡面就new一個dao類物件,呼叫,其他有意義的事沒做,也不明白有這個有什麼用,參加

spring MVC獲取requestresponse:

log clas pos stc body stat response ons style spring MVC中獲取request和response: HttpServletRequest request = ((ServletRequestAttributes)

搭建DAOService程式碼

第一部分建立實體和對映檔案 1 通過資料庫生成的實體,此步驟跳過,關於如何檢視生成反向工程實體類檢視SSH框架搭建教程-反向工程章節     Tmenu和AbstractorTmenu是按照資料庫表反向工程形成的JAVA實體; 在形成實體的時候注意 1 名稱空

spring mvccontroller怎麼接收頁面表單提交的資料

轉自:http://blog.csdn.net/wujiaqi168/article/details/41957187 1、定義一個類,該類的欄位要包含表單中所有input的name對應的值,並且在類中為對應的欄位生成getter 與setter方法 2、在對應的c

關於Daoservice的介面問題

在專案中發現Service層每個實現類都有一個介面,網上搜搜發現Dao層也存在這樣的情況。在我們專案中,我們使用的是Hibernate原生的Dao,並沒有新增額外的介面。而我們的service層中業務實現類和介面在絕大多數的業務場景中都是一一對應的,沒有介面是有多個公共實現類

spring mvccontroller使用介紹

SpringMVC是一個基於DispatcherServlet的MVC框架,每一個請求最先訪問的都是DispatcherServlet,DispatcherServlet負責轉發每一個Request請求給相應的Handler,Handler處理以後再返回相應的檢視(View

Spring MVCController如何將資料返回給頁面

要實現Controller返回資料給頁面,Spring MVC 提供了以下幾種途徑: ModelAndView:將檢視和資料封裝成ModelAndView物件,作為方法的返回值,資料最終會存到HttpServletRequest物件中! Model物件:通過給方法新增引用

junit4 測試dao service

       最近在學習ssh框架,難免會用到junit4 測試dao層 或service層,經常出現空指標。 原因:dao層和service層是[email protected]注入的,我在測試類直接ew了dao層一個物件,出現空指標錯誤,原來spring沒起作

Spring MVCController如何進行重定向

Spring MVC中進行重定向,本人知道的有兩種方式: 方法返回的URI(相對路徑)中加上"redirect:"字首,宣告要重定向到該地址 使用HttpServletResponse物件進行重定向

通過攔截器Interceptor實現Spring MVCController介面訪問資訊的記錄

java web工程專案使用了Spring+Spring MVC+Hibernate的結構,在Controller中的方法都是用於處理前端的訪問資訊,Controller通過呼叫Service進行業務處理後給前端返回ModelAndView物件或者只返回Json格式資料。如

Spring MVCController如何獲取Form表單提交的資料

表單提交的資料也就是請求資料,分為Get和Post兩種方式提交。 Controller中有三種方式獲取表單資料:  Controller的方法,新增HttpServletRequst型別入參,通過HttpServletRequst.getParameter()獲取請求資料

Spring+Hibernate+MVCController引入@Transaction對Service設計的簡化

Spring+Hibernate是目前Java應用開發中比較常見的組合,在開發WEB應用的時候,可能會結合一些其他的框架如Struts,這裡的介紹以Spring MVC為WEB框架。 目前採用的分層設計(MVC)中,資料的持久化獲取主要都是在Service中完成的,而Cont

MVC的daoservicecontroller

dao層主要做資料持久層的工作,負責與資料庫進行聯絡的一些任務都封裝在此,dao層的設計首先是設計dao層的介面,然後在Spring的配置檔案中定義此介面的實現類,然後就可以再模組中呼叫此介面來進行資料業務的處理,而不用關心此介面的具體實現類是哪個類,顯得結構非常清晰,dao層的資料來源配置,以及有關資料庫連

spring springmvc 將事物管理(@Transactional)分別加在 controllerservice

1.將事物加在contrller層,只需要在springmvc.xml中加上<tx:annotation-driven/>即可,並且contrller類中加上@Transactional即可。 前提是在spring.xml中要有事物管理器的配置即     <

在SpringBoot配置controller切面service切面的區別

其實區別很簡單,貼兩端程式碼一目瞭然service層的aoppackage com.yy.aop; //省略匯入包 @Aspect @Service("logAspect") public class LogAspect { @Pointcut("executi