• Spring-boot官方案例分析之log4j


    Spring-boot官方案例分析之log4j

      运行单元测试分析:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
    @WebAppConfiguration
    @IntegrationTest("server.port=0")
    @DirtiesContext

    进行单元测试的注解配置:

    @SpringApplicationConfiguration(classes=SampleAactuatorApplication.class)表示要启动的程序入口类

     

    @DirtiesContext当上下文遭到破坏时,会重新注册上下文

    接下来声明一个测试类:

     

    于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration

     

    @IntegrationTest("server.port=0")使用0表示端口号随机,也可以具体指定如8888这样的固定端口

     

    public class SampleActuatorApplicationTests {

       @Value("${local.server.port}")
       private int port;

       @Test
       public void testHome() throws Exception {
          @SuppressWarnings("rawtypes")
          ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
                "http://localhost:" + port, Map.class);
          assertEquals(HttpStatus.OK, entity.getStatusCode());

            System.out.println("entity.getStatusCode():"+entity.getStatusCode());
            @SuppressWarnings("unchecked")
          Map<String, Object> body = entity.getBody();
          assertEquals("Hello Phil", body.get("message"));
            System.out.println(body.get("message"));
    //
       }

    }

    注入配置文件中的值到属性port中;@Value

    声明测试类:

    @SuppressWarnings("rawtypes")

    是压制警告 

    k也可以使用@Rule

    public OutputCapture capture=new OutputCapture();

    但是在新版的Junit中,assertEquals 方法已经被废弃,它建议我们使用assertArrayEquals,旨在让我们测试一个方法的时候多传几种参数进行多种可能性测试。

    通过@Autowired可以织入容器中自己想要测试的对象。

    控制器里面定义了请求匹配的@RequestMapping

    同时注入了HelloWorldService组件,HelloWorldService注入ServiceProperties类,程序入口为SampleActuatorApplication类;

    在测试类里进行单元测试,模拟请求

    @Test
    public void TestName(){

        @SuppressWarnings("rawtypes")
        ResponseEntity<Map> entity=new TestRestTemplate().getForEntity(
                "http://localhost:" + port, Map.class);

        System.out.println(port);
        assertEquals(HttpStatus.OK,entity.getStatusCode());
      //  System.out.println("name:" +name);
        @SuppressWarnings("unchecked")
        Map<String,Object> body=entity.getBody();
        assertEquals("Hello Phil",body.get("message"));

        System.out.println(body.get("message"));


    }
  • 相关阅读:
    [JSBSim]基于winsocket2的TCPUDP使用例子
    [转][JSBSim]JSBSim的使用--飞行控制组件及其配置
    [原][译][osg][osgEarth]飞行模拟软件JSBSim的操作(FGFCS类)
    [原][JSBSim]基于qt代码实现:TCP|UDP与飞行模拟软件JSBSim的通信,现实模型飞行!
    Linux mysql添加用户,删除用户,以及用户权限
    Linux启动/停止/重启Mysql数据库的方法
    Linux中vi编辑器的使用详解
    安装mysql问题解决
    centos6.5下修改文件夹权限和用户名用户组
    mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid) 的解决方法
  • 原文地址:https://www.cnblogs.com/alliswelltome/p/5949136.html
Copyright © 2020-2023  润新知