• JUnit简单测试


    创建一个应用POJO类,也就是一个JavaBean

    package Test;
    
    //雇员类,用于取得或设置雇员的姓名、薪水、年龄
    public class EmployeeDetails {
    
        private String name;
        private double monthlySalary;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getMonthlySalary() {
            return monthlySalary;
        }
    
        public void setMonthlySalary(double monthlySalary) {
            this.monthlySalary = monthlySalary;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    




    创建一个EmpBusinessLogic类用于对年薪和评估金额的计算

    package Test;
    
    public class EmpBusinessLogic {
    
        //    计算雇员的年薪
        public double calculateYearlySalary(EmployeeDetails employeeDetails) {
            double yearlySalary = 0;
            yearlySalary = employeeDetails.getMonthlySalary() * 12;
            return yearlySalary;
        }
    
        //    计算雇员的评估金额
        public double calculateAppraisal(EmployeeDetails employeeDetails) {
            double appraisal = 0;
            if (employeeDetails.getMonthlySalary() < 10000) {
                appraisal = 500;
            } else {
                appraisal = 1000;
            }
            return appraisal;
        }
    
    
    }
    
    




    创建一个测试案例类,对评估金额和年薪进行测试

    package Test;
    
    import org.junit.Test;
    
    import static org.junit.Assert.assertEquals;
    
    public class TestEmplyeeDetails {
    
        EmployeeDetails employeeDetails = new EmployeeDetails();
        EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
    
    //    测试评估金额
        @Test
        public void testCalculateAppriasal(){
            employeeDetails.setName("杨胖子");
            employeeDetails.setAge(25);
            employeeDetails.setMonthlySalary(8000);
    
            double appraisal = empBusinessLogic.calculateAppraisal(employeeDetails);
    
            assertEquals(500,appraisal,0.0);
        }
    
    //    测试雇员的年薪
        @Test
        public void testCalculateYearSalary(){
            employeeDetails.setName("杨胖子");
            employeeDetails.setAge(25);
            employeeDetails.setMonthlySalary(8000);
    
            double salary = empBusinessLogic.calculateYearlySalary(employeeDetails);
    
            assertEquals(96000,salary,0.0);
        }
    
    
    }
    
    




    执行测试案例类

    package Test;
    
    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class TestRunner {
        public static void main(String[] args) {
            Result result = JUnitCore.runClasses(TestEmplyeeDetails.class);
    
            for(Failure failure:result.getFailures()){
                System.out.println(failure);
            }
    
            System.out.println(result.wasSuccessful());
    
    
        }
    }
    
    
  • 相关阅读:
    *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory
    linker command failed with exit code 1 (use -v to see invocation)
    谈谈我对PhoneGap的看法——(摘自唐巧的技术博客)
    ssh key一键自动化生成公钥私钥,并自动分发上百服务器免密码交互
    2018年Linux运维人员必会开源运维工具体系总结
    CentOS 6下PXE+Kickstart无人值守安装操作系统
    IOS优秀博客
    Nginx简单实现网站的负载均衡
    堆排序 Heap Sort
    h5移动端设计页面
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12724567.html
Copyright © 2020-2023  润新知