```
//省略測(cè)試類的包名和其他需要import的文件
//首先需要import待測(cè)試的controller
importcom.byx.study.repo.BreakController
/**
* BreakController Test拆分相關(guān)測(cè)試
* @author LiDan
*/
class?BreakControllerTest{
//初始化controller中引用的Service蝶念,由Spring容器注入springSecurityServvice
def springSecurityService
//這里先定義一個(gè)變量呜袁,下文中將new的controller賦值給它
def b
// setUp方法迅矛,寫(xiě)一些初始化的代碼
@Before
public void setUp(){
//該項(xiàng)目中調(diào)用springSecurity校驗(yàn)用戶是否登錄
assertFalse springSecurityService.loggedIn
//模擬用戶登錄
SpringSecurityUtils.reauthenticate"moni001",null
assertTrue springSecurityService.loggedIn
// new controller
b =new BreakUpTestController()
}
@After
public void tearDown(){
? ? SecurityContextHolder.clearContext()
}
//測(cè)試showPage頁(yè)面臭挽,action需要返回值
@Test
public void testShowPage(){
assertTrue springSecurityService.loggedIn
def result = b.breakUpTest()
assert result.schoolNo
}
//測(cè)試getList方法
@Test
public void testGetList(){
assertTrue springSecurityService.loggedIn
//修改controller實(shí)例的params屬性的值,用來(lái)模擬通過(guò)ajax向controller提交數(shù)據(jù)
b.params.rows =10
b.params.page =1
b.getTestListAjax()
assert breakUpTestController.response.json.total
}
}
```
本文引自:http://www.voidcn.com/blog/ld_____/article/p-5738122.html