单元测试

 

一 、Junit
 
1、环境配置

     使用idea IDE 进行单元测试,首先需要安装JUnit 插件。

          1.安装JUnit插件步骤

              File-->settings-->Plguins-->Browse repositories-->输入JUnit-->选择JUnit Generator V2.0安装(可以自动生成测试代码)。

          2.使用JUnit插件

             在需要进行单元测试的类中,在需要测试的类文件里点击code->Generate(使用快捷键alt+insert,选择JUnit test,选择JUnit4。
 
注意:IDEA自带的JUnit插件和JUnitGeneratorV2.0插件都要勾选上,若只勾选JUnit可能导致无法自动生成测试文件,若只勾选JUnitGenerator V2.0可能导致生成的测试文件无法运行
 
2、生成测试代码
选中要测试的类,右键—>go to —>指定生成到测试类test

 

生成的测试类就放到Test下面:

 

3、写测试代码
常用注解:
(1)@BeforeClass所修饰的方法在所有方法加载前执行,而且他是静态的在类加载后就会执行该方法,在内存中只有一份实例,适合用来加载配置文件。
(2)@AfterClass所修饰的方法在所有方法执行完毕之后执行,通常用来进行资源清理,例如关闭数据库连接。
(3)@Before和@After在每个测试方法执行前都会执行一次。
(4)@Test(excepted=XX.class) 在运行时忽略某个异常。
(5)@Test(timeout=毫秒) 允许程序运行的时间。
(6)@Ignore 所修饰的方法被测试器忽略。
(7)RunWith 可以修改测试运行器 org.junit.runner.Runner
 

Assert类中定义了很多静态方法来进行断言。列表如下:

  • assertTrue(String message, boolean condition) 要求condition == true
  • assertFalse(String message, boolean condition) 要求condition == false
  • fail(String message) 必然失败,同样要求代码不可达
  • assertEquals(String message, XXX expected,XXX actual) 要求expected.equals(actual)
  • assertArrayEquals(String message, XXX[] expecteds,XXX [] actuals) 要求expected.equalsArray(actual)
  • assertNotNull(String message, Object object) 要求object!=null
  • assertNull(String message, Object object) 要求object==null
  • assertSame(String message, Object expected, Object actual) 要求expected == actual
  • assertNotSame(String message, Object unexpected,Object actual) 要求expected != actual
  • assertThat(String reason, T actual, Matcher matcher) 要求matcher.matches(actual) == true

 

4、私有方法的单元测试
一般只有public修饰的方法才可以写单元测试,如果想要对私有的方法也做单元测试,可以用invoke(类反射):
实现方法:
public Object invoke(Object obj,Object... args)throws IllegalAccessException,IllegalArgumentException,InvocationTargetException
用法:
(1)获取该类的Class Type;
(2)通过getMethod方法获取Method对象;
(3)通过调用invoke方法来执行对象的某个方法:invoke(class, method)
 
例如对下面的私有方法进行单元测试:
private void validateArg(NightFreqInput param) throws IOException {
        if (param == null) {
            throw new IOException("RelationshipParam Exception: Param is null");
        } else {
            if (StringUtils.isEmpty(param.getStartTime())) {
                throw new IllegalArgumentException("StartTime can't be empty!");
            }
            if (StringUtils.isEmpty(param.getEndTime())) {
                throw new IllegalArgumentException("EndTime can't be empty!");
            }
    }
    }

 

 @Test
    public void validateArgTest() throws Exception {
     NightFreqInput param = new NightFreqInput();
     param.setStartTime("2018-08-10 00:00:00");
     param.setEndTime("2018-08-10 23:59:59");
     Class clazz = Class.forName("com.*.*.hbase.NightFreqSearch.NightFreqSearch");
     NightFreqSearch nfs = new NightFreqSearch();
     Method m = clazz.getDeclaredMethod("validateArg",NightFreqInput.class); //第一个参数是方法的名字,第二个参数是方法的参数的class type
     m.setAccessible(true);//Accessable属性是继承自AccessibleObject 类. 功能是启用或禁用安全检查,setAccessible(true)则不启用安全检查

     m.invoke(nfs,param);
    }

 

 有关mock的部分参见:

https://www.ibm.com/developerworks/cn/java/j-easymock.html?spm=a2c4e.11153940.blogcont44685.10.12a9665aivnitV

 

 

 

 

 

 

 

 

 

 

 
posted on 2019-05-10 09:45  小驼驼  阅读(173)  评论(0编辑  收藏  举报