zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    大教堂和市集
    VB相关资源
    什么样的团队才是优秀的团队
    走过2007
    一种简单实用的错误码构建方法
    熊的困惑
    调测中的反思
    PowerPC852T的SMC初始化—串口通信
    Silverlight Listbox 取消全选的方法
    电脑读不出U盘,有一个黄色感叹号的解决方法
  • 原文地址:https://www.cnblogs.com/dousil/p/14506122.html
Copyright © 2011-2022 走看看