Junit框架测试

Junit 框架概述:

Junit 可以选择执行哪些测试方法,也可以一键执行全部的测试方法

Junit 可以生成测试报告,测试无误为绿色,测试失败为红色,有返回值类型的方法可以加入断言

单个方法的测试失败不会影响其他的方法

实现要求:

测试方法必须是公共的无参无返回值类型的非静态方法

测试方法上标注@Test

Junit 常用注解:
注解Junit 4Junit 5说明
@Test测试方法
@Before@BeforeEach用来修饰实例方法,该方法会在每一个测试方法执行前执行一次
@After@AfterEach用来修饰实例方法,该方法会在每一个测试方法执行后执行一次
@BeforeClass@BeforeAll用来静态修饰方法,该方法会在所有测试方法之前只执行一次
@AfterClass@AfterAll用来静态修饰方法,该方法会在所有测试方法之后只执行一次

before 初始化资源

after 释放资源

代码实现:
测试类代码实现:
package com.xm;

import org.junit.*;

public class Demo1 {

    @Before
    public void before()
    {
        System.out.println("before方法执行一次");
    }
    @After
    public void after()
    {
        System.out.println("after方法执行一次");
    }
    @BeforeClass
    public static void BeforeClass()
    {
        System.out.println("beforeClass执行一次");
    }
    @AfterClass
    public static  void AfterClass()
    {
        System.out.println("AfterClass执行一次");
    }




    @Test
    public void test1()
    {
        Server server = new Server();
         String str=server.loginName("admin");
        Assert.assertEquals("功能出现问题","登录成功",str);
    }
    @Test
    public void test2()
    {
        Server server = new Server();
        server.selectName();
    }
}

需要测试的类:
package com.xm;

public class Server {
    public String loginName(String name)
    {
        if("admin".equals(name))
            return "登录成功";
        else
            return "用户名有问题";
    }
    public void selectName()
    {
        System.out.println(10/1);
        System.out.println("查询完毕");
    }
}
模拟Junit框架 代码实现:
package com.xm;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestJunit {
    @MyTest
    public void test1() {
        System.out.println("test1");
    }

    public void test2() {
        System.out.println("test2");
    }

    @MyTest
    public void test3() {
        System.out.println("test3");
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<TestJunit> c1 = TestJunit.class;
        Method[] methods = c1.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(MyTest.class)) {
                Constructor<TestJunit> constructor = c1.getConstructor();
                TestJunit junit = constructor.newInstance();
                method.invoke(junit);

            }
        }
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyTest {

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ILFFQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值