简体   繁体   English

JUnit在Netbeans上测试一个类

[英]JUnit testing a class on Netbeans

Could someone create a JUnit test in NetBeans for the code that I have pasted below? 有人可以在NetBeans中为我下面粘贴的代码创建JUnit测试吗? I'm not quite sure what to do once I've created the actual test. 创建完实际测试后,我不确定该怎么做。

package prog3;     

import java.util.ArrayList;     import java.util.Iterator;

public class MedicineClass
{
    private String MedicineName;
    private int MedicineRegNum;
    private ArrayList RelatedMedicine;

    public MedicineClass(String newMedicineName, int newMedicineRegNum)
    {
        newMedicineName = MedicineName;
        newMedicineRegNum = MedicineRegNum;
        RelatedMedicine = new ArrayList();
    }

    public void setMedicine(String newMedicineName)
    {
        newMedicineName = MedicineName;
    }

    public String getMedicineNameAndNum()
    {
        return "Medicine Registration Number: " + MedicineRegNum + "Medicine Name: " + MedicineName;
    }

    public void Medicine(Medicine Drug)
    {
        RelatedMedicine.add(Drug);
    }

    public void addMedicine(String newMedicineName, int newMedicineRegNum)
    {
        Medicine temp = new Medicine(newMedicineName, newMedicineRegNum);
        RelatedMedicine.add(temp);
    }

    public void listAllMedicines()
    {
        StringBuilder sb = new StringBuilder();
        Iterator lst = RelatedMedicine.iterator();
        while (lst.hasNext())
        {
            Medicine temp = (Medicine)lst.next();
            sb.append(temp.getNameandRegNum());
            sb.append("\n");

        }
        System.out.println(sb.toString());
    }

}

You have the class that you need to write JUnit tests for. 您具有编写JUnit测试所需的类。 Read this: Writing JUnit Tests in NetBeans IDE and Introduction to Unit Testing 阅读本文: 在NetBeans IDE中编写JUnit测试单元测试简介

I suggest using the "create JUnit test" button in the IDE. 我建议在IDE中使用“创建JUnit测试”按钮。 This will place the class in the right place for you. 这样可以将课程放置在适合您的位置。 You can then copy'n'paste your code into that test. 然后,您可以将代码粘贴到该测试中。 If that button is not visible on your toolbar, right-click on your toolbar and add that button from the customise menu. 如果该按钮在工具栏上不可见,请在工具栏上单击鼠标右键,然后从自定义菜单中添加该按钮。 I also recommend adding the "test this class" button. 我还建议添加“测试此类”按钮。

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

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