1春ブート・スターターテスト建てmockito、依存関係ポンポンを追加
<依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータ試験</たartifactId> <スコープ>テスト</スコープ> </依存>
実施例2コントローラ
パッケージcom.shangcg.controller。 インポートのjavax.servlet.http.HttpServletRequest; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RequestMethod; 輸入org.springframework.web.bind.annotation.RequestParam; 輸入org.springframework.web.bind.annotation.RestController; / * * * @version v1.0の * @description:JUnitの和モック单元测试示例 * @Author:shangcg * @Date:2019年12月24日 * / @RestController パブリック クラスUnitDemoController { @RequestMapping(値 = 「/こんにちは。 JSON "、方法= RequestMethod.GET) 公共の文字列getListTag(HttpServletRequestのリクエスト、 @RequestParam(値 = " 名前"、必要= falseを、はdefaultValue = " 0 " )文字列名){ 試み{ リターン " こんにちは:" + 名。 } キャッチ(例外e){ e.printStackTrace(); } リターン 「ハローみんな!」。 } @RequestMapping(値 = " /save.json "、メソッド= RequestMethod.POST) パブリック文字列saveTag(HttpServletRequestのリクエスト、 @RequestParam(値 = " 名前"、必須= 真)文字列名、 @RequestParam(値 = " レベル"、必須= 真)整数レベル){ 試み{ リターン " あなたのparamをrecive " + " 名:" +名+ " レベル:" + レベル。 }キャッチ(例外e){ e.printStackTrace(); } 戻り ヌル。 } }
実施例3試験クラス
パッケージcom.shangcg.controller。 輸入静的 org.junit.Assert *。; 輸入org.junit.Assert; 輸入org.junit.Before; 輸入org.junit.Test; 輸入org.junit.runner.RunWith; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 輸入org.springframework.boot.test.context.SpringBootTest; 輸入org.springframework.http.MediaType。 輸入org.springframework.test.context.junit4.SpringRunner; 輸入org.springframework.test.web.servlet.MockMvc; 輸入org.springframework.test.web.servlet.MvcResult; 輸入org.springframework.test.web.servlet.request.MockMvcRequestBuilders。 輸入org.springframework.test.web.servlet.result.MockMvcResultHandlers。 輸入org.springframework.test.web.servlet.result.MockMvcResultMatchers。 輸入org.springframework.test.web.servlet.setup.MockMvcBuilders。 輸入org.springframework.web.context.WebApplicationContext; / * * * @version v1.0の * @description:TODO * @Author:shangcg * @Date:2019年12月24日 * / @RunWith(SpringRunner クラス) @SpringBootTest @AutoConfigureMockMvc パブリック クラスUnitDemoControllerTest { @Autowired 民間 WebApplicationContext webApplicationContext ; 民間 MockMvc mockMvc。 @Before 公共 ボイドセットアップ(){ mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); // 建议使用这种 } @Test // 对GET方法的测试 公共 ボイドtestGetListTag()例外{スロー MvcResult mvcResult = mockMvc.perform( MockMvcRequestBuildersを。得る(" /hello.json " ) .contentType(MediaType.APPLICATION_JSON)。 (characterEncoding "UTF-8 "(mockMvc.perform) .PARAM(" 名前"、" shangcg " ) ).andExpect(MockMvcResultMatchers.status()ISOK())。 .andDo(MockMvcResultHandlers.print()) .andReturn(); 文字列の内容 = mvcResult.getResponse()getContentAsString()。 Assert.assertEquals(" こんにちは:shangcg " 、コンテンツ)。 } @Test // 对ポスト测试 公共 ボイドsaveTag()は、{例外をスロー MvcResult mvcResult = MockMvcRequestBuilders.post(" /save.json " ) .contentType(MediaType.APPLICATION_JSON) .PARAM(" 名前"、" shangcg " ) .PARAM(" レベル"、" 1 " ) ).andExpect(MockMvcResultMatchers.status()。 ISOK()) .andDo(MockMvcResultHandlers.print()) .andReturn(); 文字列の内容 = mvcResult.getResponse()getContentAsString()。 Assert.assertEquals(" あなたのparamの名前recive:shangcgレベル:1 " 、 } }
4個の結果が返されます
サンプルプロジェクトのコードの場合5は、より多くの、メッセージの必要性を残してくださいソースコードをアップロードすることはできません