• idea 如何快速测试


    大项目,即使微服务,用@PostConstructor 或者,写main ,编译耗时,代码不纯洁。最号的方法Junit测试

    这里介绍快捷的测试方法,

     生成代码如下:

    package com.uxsino.commons.cache;
    
    
    import org.junit.Test;
    
    public class ItemTest {
        @Test
        public void test(){
            Item item = new Item();
            item.setDatas(new String ("dousil"));
            System.out.println(item.getDatas());
        }
    }
    

      快捷方式2 ,Alt+shift +t 可以直接生成测试类。

    Junit 缺陷,junit 在测试的时候,无法知道我们是否使用spring 框架。更不用说,帮我们创建spring 容器了。

    利用spring test 就可以很好的实现。

    1. 整合junit的必备jar包

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
    </dependency>

    2. 使用@RunWith注解

    @RunWith(SpringJUnit4ClassRunner.class)
    public class AccountServiceTest {
    }
    

      3. 使用ContextConfiguration 指定配置文件位置

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= {"classpath:applicationContext.xml"})
    public class AccountServiceTest {
    }
    

      

    @ContextConfiguration 注解:

    • locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明
    • classes 属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置

    4. 使用@Autowired 注解

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= {"classpath:bean.xml"})
    public class AccountServiceTest {
    	@Autowired
    	private IAccountService as ;
    }
    

      示例如下:

    package com.uxsino.simo.collector;
    
    import static org.junit.Assert.fail;
    
    import org.junit.Ignore;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class)
    public class TestExecEnvironment {
    
    	@Configuration
    	 @ComponentScan("com.uxsino.simo.collector")
    	public static class Config {
    		@Autowired
    		ExecEnvironment env;
    	}
    
        @Test
        public void testExecEnvironment() {
            //assert (env != null);
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testGetNameSpace() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testGetEntityDomain() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testInit() {
            //NEDBStorage entitydb = new NEDBStorage();
            ExecEnvironment execEnvironment = new ExecEnvironment();
            assert (execEnvironment != null);
            execEnvironment.init();
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testDumpTasks() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testRunPatrolTask() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testStoreToDb() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testRunImmediateQueryEntityInfoIndicator() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testRunImmediateQueryStringEntityInfoIndicator() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testExecQuery() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testSubscribeValueConsumerOfIndicatorValue() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testSubscribeValueSubscriberOfQsuperIndicatorValue() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testAddTask() {
            fail("Not yet implemented");
        }
    
        @Ignore("Test is ignored before immplemenation")
        @Test
        public void testDeleteTask() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testX() {
    
    //        SSHTarget target = new SSHTarget();
    //        target.host = "localhost";
    //        target.port = 22;
    //        target.passwordEncrypted=false;
    //        target.setUsername("testcon");
    //        target.setPassword("Pdf1scape");
    //
    //        SSHConnection conn = new SSHConnection();
    //
    //        conn.connect(target);
    //
    //        for (int i = 0; i < 5; i++) {
    //            conn.execCmd("date");
    //        }
    //
    //        conn.close();
        }
    
    }
    

      


    ————————————————
    版权声明:本文为CSDN博主「不爱我就写代码」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_41744145/article/details/100125690

  • 相关阅读:
    使用python脚本自动创建pip.ini配置文件
    Windows 安全软件集
    配置 vim 编辑器
    (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)
    转 10 个最佳的 Node.js 的 MVC 框架
    转-基于NodeJS的14款Web框架
    (转) [it-ebooks]电子书列表
    (原创) alljoyn物联网实验之手机局域网控制设备
    (原创)天气又转热了,我给她做了个智能小风扇
    Alljoyn瘦客户端库介绍(官方文档翻译 下)
  • 原文地址:https://www.cnblogs.com/dousil/p/14506122.html
Copyright © 2020-2023  润新知