zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    Java 基本数据类型
    关于 Java 安装配置文件总结
    Day01
    关于自律!
    Java
    Java
    一年软件开发工作有感!
    如何解决文档复制时候禁止复制限制
    tensorflow tf.keras概述
    jupyter使用说明书
  • 原文地址:https://www.cnblogs.com/yaos/p/14014300.html
Copyright © 2011-2022 走看看