SpringBoot整合Junit测试 |
您所在的位置:网站首页 › spring启动器 › SpringBoot整合Junit测试 |
SpringBoot整合Junit测试
原创
霸道流氓 2023-03-13 09:25:26 博主文章分类:SpringBoot ©著作权 文章标签 springBoot+Junit spring Test 运行测试 文章分类 HarmonyOS 后端开发 ©著作权归作者所有:来自51CTO博客作者霸道流氓的原创作品,请联系作者获取转载授权,否则将追究法律责任 实现在项目的pom/xml中添加测试依赖 org.springframework.boot spring-boot-starter-test test junit junit test
在controller包下新建要进行测试的controller。 SpringController package com.example.demo.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; @Controller@EnableAutoConfigurationpublic class SpringController { @RequestMapping("/test") @ResponseBody public String yes() { return "test"; } public static void main(String[] args) { SpringApplication.run(SpringController.class, args); }} 在test包下新建testController 代码 package com.badao.test;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration; import com.example.demo.controller.SpringController; import junit.framework.TestCase; @SpringBootTest(classes=SpringController.class)//要测试谁@RunWith(SpringJUnit4ClassRunner.class) //指明进行测试的类@WebAppConfiguration //指明和Web的整合public class TestController { @Autowired private SpringController springController; @Test public void test1() { TestCase.assertEquals(this.springController.yes(),"test"); } } 这里使用断言比较返回值是否相等。 右键运行测试类 可以看到测试结果两个值相等。 上一篇:Netty中实现多客户端连接与通信-以实现聊天室群聊功能为例(附代码下载) 下一篇:ProtoBuf在使用protoc进行编译时提示: Required fields are not allowed in proto3 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |