`

sprintboot-第四章SpringBoot2.0单元测试进阶实战和自定义异常处理

 
阅读更多
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AutogenprojectApplication.class)
public class AutogenprojectApplicationTests {

   @Test
public void test1() {
      System.out.println("1");
      TestCase.assertEquals(1,0);
   }
   @Test
public void test2() {
      System.out.println("2");
      TestCase.assertEquals(1,1);
   }
   @Before
public void testA()
   {
      System.out.println("before");
   }
   @After
public void testB()
   {
      System.out.println("After");
   }

 

MockMvc测试

perform:执行一个RequestBuilder请求

andExpect:添加ResultMatcher->MockMvcResultMatchers验证规则

andReturn:最后返回相应的MvcResult->Response

public class MockMVCTest {

    @Autowired
private MockMvc mockMvc;
    @Test
public void test1 () throws Exception
    {
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/a/a")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
            TestCase.assertEquals(mvcResult.getResponse().getStatus(),200);

    }
}

 banner修改,在classpath的根目录下添加一个banner.txt的文件即可不需要配置application.properties,如果是其他名子需要在application.properties中增加配置spring.banner.location=banner1.txt

banner1.txt的内容如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::你是我的小苹果(v2.1.3.RELEASE)

 

以debug的形式启动应用 java -jar xxx.jar --debug

 

全局异常处理

 

@RestControllerAdvice
public class CustomerExtHanlder {
    private static final Logger LOG  = LoggerFactory.getLogger(CustomerExtHanlder.class);
    @ExceptionHandler(Exception.class)
    public Object exceptionHandler(Exception e , HttpServletRequest request)
    {
        LOG.error("error:{},utl{}",e.getMessage(),request.getRequestURL());
        Map map = new HashMap();
        map.put("status",200);
        map.put("error",e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    @ExceptionHandler(MyException.class)
    public Object exceptionHandler2(Exception e , HttpServletRequest request)
    {
        LOG.error("error:{},utl{}",e.getMessage(),request.getRequestURL());
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error.html");
        mav.addObject("msg",e.getMessage());
        return mav;
    }
}

 

 

 

分享到:
评论

相关推荐

    archive_ springboot-plus后台管理系统 v2.0 [江西新余电信].zip.zip

    SpringBoot-Plus后台管理系统是一款基于SpringBoot框架构建的企业级管理平台,其V2.0版本的发布,为开发者带来了更加高效和稳定的解决方案。这个系统以其模块化的设计、强大的功能以及易于扩展的特性,深受开发者的...

    Java开发案例-springboot-51-Prometheus+Grafana监控SpringBoot-源代码+文档.rar

    Java开发案例-springboot-51-Prometheus+Grafana监控SpringBoot-源代码+文档.rar Java开发案例-springboot-51-Prometheus+Grafana监控SpringBoot-源代码+文档.rar Java开发案例-springboot-51-Prometheus+Grafana...

    Java开发案例-springboot-66-自定义starter-源代码+文档.rar

    Java开发案例-springboot-66-自定义starter-源代码+文档.rar Java开发案例-springboot-66-自定义starter-源代码+文档.rar Java开发案例-springboot-66-自定义starter-源代码+文档.rar Java开发案例-springboot-66-...

    Springboot-的各种demo.rar

    Springboot-log 是给项目添加log 管理 Springboot-mybatis-config 这个是mybatis 的...SpringBoot-multi-source SpringBoot 多数据源配置,全局异常自定义处理 SpringBoot2-mybatisplus SpringBoot 与mybatis-plus 整

    Java开发案例-springboot-23-自定义注解实现post请求接收单个参数值-源代码+文档.rar

    Java开发案例-springboot-23-自定义注解实现post请求接收单个参数值-源代码+文档.rar Java开发案例-springboot-23-自定义注解实现post请求接收单个参数值-源代码+文档.rar Java开发案例-springboot-23-自定义注解...

    springboot-mybatis(1).rar

    - test目录:存放单元测试代码。 六、实践应用 通过这个项目,开发者可以学习到如何在SpringBoot项目中整合Mybatis,理解Spring MVC的工作原理,以及如何利用Redis优化数据访问。这对于提升个人技能和解决实际问题...

    基于springboot-vue.js的购票系统.zip

    基于springboot-vue.js的购票系统基于springboot-vue.js的购票系统 基于springboot-vue.js的购票系统基于springboot-vue.js的购票系统 基于springboot-vue.js的购票系统基于springboot-vue.js的购票系统 基于...

    springboot-activiti-test-master.zip

    而Activiti则是一个基于BPMN 2.0规范的流程引擎,能够处理复杂的业务流程,提供图形化建模工具,支持流程的运行时管理和历史审计。 在SpringBoot项目中整合Activiti,我们需要以下步骤: 1. **添加依赖**:在`pom....

    SpringBoot项目基于springboot-vue的毕业论文管理系统.zip

    SpringBoot项目基于springboot-vue的毕业论文管理系统.zipSpringBoot项目基于springboot-vue的毕业论文管理系统.zipSpringBoot项目基于springboot-vue的毕业论文管理系统.zipSpringBoot项目基于springboot-vue的毕业...

    springboot-learning-example

    9. **单元测试与集成测试**:SpringBoot提供良好的测试支持,`@SpringBootTest`注解可以帮助你编写针对整个Spring应用的测试。 学习这个项目时,你可以逐步理解每个组件的作用,尝试修改配置,观察应用的行为变化。...

    SpringBoot-2.0.rar,深度掌握-Springboot-实践技术1:基础入门

    SpringBoot 2.0引入了响应式编程模型,WebFlux提供了非阻塞的Web处理,适合处理高并发场景,与Spring MVC并存。 7. **Java 9支持**: 支持Java 9模块化系统,使得SpringBoot应用能在Java 9及以上版本中顺利运行。...

    Android代码-springboot-integration-examples

    > 工作一年以上的小伙伴可以加这个交流群,群内有免费公开课,现在加入就送Java架构师进阶系列电子书籍: springboot-integration-examples SpringBoot和其他常用技术的整合,可能是你遇到的讲解最详细的学习案例,力争...

    springboot-mybatis-0.0.1-SNAPSHOT.jar

    主要用来测试 docker部署springboot的jar包

    Springboot-Notebook 一个以 springboot 为-Springboot-Notebook.zip

    Springboot-Notebook 一个以 springboot 为-Springboot-Notebook

    springboot-elasticsearch-master.rar

    7. **测试与调试**:利用Spring Boot的测试支持,我们可以编写单元测试和集成测试,验证Elasticsearch操作的正确性。 综上所述,"springboot-elasticsearch-master"项目是一个实用的起点,可以帮助开发者快速搭建一...

    springBoot-activiti-modeler-master.zip

    Activiti是一款开源的工作流引擎,它支持BPMN 2.0标准,可以处理复杂的业务流程。Activiti提供了强大的API和直观的模型设计工具,使得开发者能够轻松地创建和执行业务流程。 二、SpringBoot集成Activiti 1. 添加...

    springboot-mybatis-tx

    springboot-mybatis-tx,springboot-mybatis-tx,springboot-mybatis-tx,springboot-mybatis-tx,springboot-mybatis-tx

    springboot-flowable-cims基于工作流引擎flowable、spring boot开发的工作流服务平台

    springboot-flowable-cims基于工作流引擎flowable、spring boot开发的工作流服务平台,完美整合官方flowable modeler 进行流程设计、表单设计。提供flowable rest api 接口 供后台业务系统流程调用。提供前后端分离...

    springboot-push.zip

    每个推送服务的集成都需要对相关平台的API和SDK有深入理解,同时还需要考虑推送策略、消息格式、错误处理等多个方面。通过学习和实践这些示例,开发者能够更好地掌握如何在Spring Boot环境中构建高效、稳定的推送...

    springBoot-demos:springBoot-demos基于1.5.x版本

    4.springboot-redis 5springboot-quartz-cluster 6.springboot-shiro 7.springboot-admin 8.springboot-html 9.springboot-html 10.springboot-cros 11.springboot-elk 12.springboot配置-集成nacos 13.springboot...

Global site tag (gtag.js) - Google Analytics