javaWeb(一)之单元测试篇

一、junit的使用
    * 单元测试

    * 测试对象是 是一个类中的方法

    * juint不是javase的一部分,想要使用导入jar包
        ** 但是,在myeclipse中自带了junit的jar包
    
    * 首先junit版本 3.x 4.x
        * 单元测试方法时候,方法命名规则 public void 方法名() {}
    
    * 使用注解方式运行测试方法, 在方法的上面
        ** @Test:表示方法进行单元测试

        ---     @Test
            public void testAdd1() {
                TestJunit test01 = new TestJunit();
                test01.testAdd(2, 3);
            }
            - 选中方法名称,右键运行 点击run as --- junit  test
            - 当出现绿色条,表示方法测试通过
            - 当出现了红棕色条,表示方法测试不通过

        --- 要运行类中的多个测试方法,点击类中的其他位置,run as --- junit  test
    
        ** @Ignore :表示这个方法不进行单元测试

        ** @Before: 在每个方法执行运行
        ** @After:在每个方法之后运行

        ** 断言(了解)
            - Assert.assertEquals("测试期望的值", "方法运行的实际的值")

二、单元测试错误集合

一、假如出现method initializationerror not found,open the test class错误

首先、注释所有其他又@test注解的方法

然后,检查,你需要使用单元测试的方法是否添加@test注解,并且是否符合上述方法命名规则。

注意检查是否导入正确的包,

猜你喜欢

转载自blog.csdn.net/rngweskt/article/details/83859484