繁体   English   中英

我有两种方法需要使用JUnit测试进行测试。 这是代码

[英]I have two methods that need to be tested using the JUnit test. Here are the codes

公共类经理扩展员工{

/**
 * Manager Constructor takes in arguements and calls superclass
 * @param id
 * @param weeklyPay
 */
public Manager(int id, double weeklyPay) // Constructor
{
    super(id);
    super.weeklyPay=weeklyPay;
}

public String toString() // returns a String
{
    return "Manager "+super.toString();
}

public double calculateWeeklyPay() // calculates weeklypay
{
    return super.weeklyPay;
}

我一定要考toString方法和calculateWeeklyPay方法。 我该怎么做?

不确定您是否要测试该类,因为超级类中的逻辑看起来很多。 但是这里...

assertEquals(42d, new Manager(1, 42d).calculateWeeklyPay,0.001);

并测试字符串是否等于(假设super.toString返回MyString

assertEquals("ManagerMyString", new Manager(1, 42d));

右侧的toString会自动调用,或者如果您想显式地调用

assertEquals("ManagerMyString", new Manager(1, 42d).toString());

所有这些方法都是JUnit的一部分,可以通过Assert类静态导入到Java文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM