• Unit Testing of Classes in Java


    Every class can have a main method. That is a handy trick for unit testing of classes. For example, you can add a main method to the Employee class:

    test.java :

    import java.util.*;
    import java.time.*;
    
    class Employee
    {
    	String name;
    	double salary;
    	LocalDate hireday;
    
    	public Employee(String n, double s, int year, int month, int day)
    	{
    		name = n;
    		salary = s;
    	}
    
    	public static void main(String[] args)
    	{
    		Employee e = new Employee("Romeo", 50000, 2003, 3, 31);
    		System.out.println(e.name);
    	}
    }
    

    After compilation javac test.java:

    If you want to test the Employee class in isolation, simply execute

    java Employee
    

    If the Employee is a part of a larger application, you start the application with

    java Application
    

    and the main method of the Employee class is never executed.

    这个方法可以用来调试Java子程序和Class,而不用每次都新建一个完整的Application。

  • 相关阅读:
    作业07-Java GUI编程
    作业06-接口、内部类
    作业05-继承、多态、抽象类与接口
    作业14-数据库
    作业13-网络
    作业12-流与文件
    作业11-多线程
    作业10-异常
    作业09-集合与泛型
    作业08-集合
  • 原文地址:https://www.cnblogs.com/yaos/p/7088864.html
Copyright © 2020-2023  润新知