基于junit的单元测试,请大家帮我设计这个计算器的测试用例

向大牛学习人工智能 2010-05-03 08:31:17

import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator
implements ActionListener { //导入动作监听接口
//设计面板中的单位
JFrame frame;//主窗口
JTextField textAnswer;//显示文本框
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JTextField textMemory;
JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
JButton buttonBk, buttonCe, buttonC;
JButton button[];
JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDao, buttonEqual;
JMenu viewMenu, helpMenu;
JMenuItem tItem, sItem, topHelp, aboutCal;
DecimalFormat df; //设置数据输出精度
boolean clickable; //控制当前能否按键
double memoryd; //使用内存中存储的数字
int memoryi;
double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
String copy; //做复制用
JTextArea help; //帮助
JScrollPane scrollHelp;
//构造函数
public Calculator() {
clickable = true;
answerd = 0;
frame = new JFrame("计算器");
df = new DecimalFormat("0.00000000"); //设置数据输出精度(对于double型值)
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setHorizontalAlignment(JTextField.RIGHT);
textAnswer.setBorder(BorderFactory.createLineBorder(Color.red));
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
panel = new JPanel();
frame.getContentPane().add(panel);
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
//设计整个面板
mainMenu = new JMenuBar();
viewMenu = new JMenu("查看(V)");
helpMenu = new JMenu("帮助(H)");

tItem = new JMenuItem("●标准型(T)");
tItem.addActionListener(this);
sItem = new JMenuItem("●科学型(S)");
sItem.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);

topHelp = new JMenuItem(" 帮助主题(H)");
topHelp.addActionListener(this);
help = new JTextArea(5, 20);
scrollHelp = new JScrollPane(help);
help.setEditable(false);
help.append("执行简单计算\n");
help.append("1. 键入计算的第一个数字。\n");
help.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。\n");
help.append("3. 键入计算的下一个数字。\n");
help.append("4. 输入所有剩余的运算符和数字。\n");
help.append("5. 单击“=”。\n");
aboutCal = new JMenuItem(" 关于计算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
panel.add(mainMenu, BorderLayout.NORTH);
panel.add(textAnswer, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
textMemory = new JTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(new Color(217, 217, 217));
labelMemSpace = new JLabel(" ");
buttonBk = new JButton("Backspace");
buttonBk.setForeground(new Color(255, 0, 0));
buttonCe = new JButton("CE");
buttonCe.setForeground(new Color(255, 0, 0));
buttonC = new JButton("C");
buttonC.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2, BorderLayout.NORTH);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel3 = new JPanel();
panel1.add(panel3, BorderLayout.CENTER);
button = new JButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i));
button[i].setForeground(new Color(0, 0, 255));
}
buttonMC = new JButton("MC");
buttonMC.setForeground(new Color(255, 0, 0));
buttonMR = new JButton("MR");
buttonMR.setForeground(new Color(255, 0, 0));
buttonMS = new JButton("MS");
buttonMS.setForeground(new Color(255, 0, 0));
buttonMAdd = new JButton("M+");
buttonMAdd.setForeground(new Color(255, 0, 0));
buttonDot = new JButton(".");
buttonDot.setForeground(new Color(0, 0, 255));
buttonAddAndSub = new JButton("+/-");
buttonAddAndSub.setForeground(new Color(0, 0, 255));
buttonAdd = new JButton("+");
buttonAdd.setForeground(new Color(255, 0, 0));
buttonSub = new JButton("-");
buttonSub.setForeground(new Color(255, 0, 0));
buttonMul = new JButton("*");
buttonMul.setForeground(new Color(255, 0, 0));
buttonDiv = new JButton("/");
buttonDiv.setForeground(new Color(255, 0, 0));
buttonMod = new JButton("%");
buttonMod.setForeground(new Color(0, 0, 255));
buttonSqrt = new JButton("sqrt");
buttonSqrt.setForeground(new Color(0, 0, 255));
buttonDao = new JButton("1/x");
buttonDao.setForeground(new Color(0, 0, 255));
buttonEqual = new JButton("=");
buttonEqual.setForeground(new Color(255, 0, 0));
//将所有行为与监听绑定
panel3.setLayout(new GridLayout(4, 6));
panel3.add(buttonMC);
buttonMC.addActionListener(this);
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(buttonSqrt);
buttonSqrt.addActionListener(this);
panel3.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(buttonMod);
buttonMod.addActionListener(this);
panel3.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(buttonDao);
buttonDao.addActionListener(this);
panel3.add(buttonMAdd);
buttonMAdd.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonAddAndSub);
buttonAddAndSub.addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}

...全文
946 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
che253604783 2010-05-11
  • 打赏
  • 举报
回复
楼主,不好测的,你的都是写在一个方法里面的。要分开到各个业务方法里面去,才好测。不过不明白楼主的用意,如果是想学JUnit可以找个其他例子测,如果是想测试计算器的功能的话,可以直接黑盒测试。
  • 打赏
  • 举报
回复
高手请进啊!!!!
yuanlaifenglin 2010-05-08
  • 打赏
  • 举报
回复
简单工厂???
一头头 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 tyuiouio 的回复:]

如何降低我的程序的耦合度呢?请高手帮忙,谢谢
[/Quote]

OOP啊
  • 打赏
  • 举报
回复
如何降低我的程序的耦合度呢?请高手帮忙,谢谢
  • 打赏
  • 举报
回复

public class Computer{
private int a;
private int b;
public Computer(int x, int y)
{
a=x;
b=y;
}
public int add()
{
return a+b;
}
public int minus()
{
return a-b;
}
public int multiply()
{
return a*b;
}
public int divide()
{
if(b!=0)
return a/b;
else
return 0;
}
Public int sqrt()
{
If(a<0)
System.out.println(“函数输入无效”);
Else
Return
}
}


如何把上面这段代码加入计算器的java文件中
取你猪头 2010-05-08
  • 打赏
  • 举报
回复
围观,接分!谢谢
  • 打赏
  • 举报
回复
具体如何实现呢?请高手帮忙
[Quote=引用 14 楼 tyuiouio 的回复:]
Java code

public class Computer{
private int a;
private int b;
public Computer(int x, int y)
{
a=x;
b=y;
}
public int add()
{
return a+b;
}
public int minus()
{
return a-b;
}
public……
[/Quote]
  • 打赏
  • 举报
回复

[Quote=引用 16 楼 coldanimal 的回复:]
引用 15 楼 tyuiouio 的回复:

如何降低我的程序的耦合度呢?请高手帮忙,谢谢


OOP啊
[/Quote]

具体如何实现呢?请高手帮忙
  • 打赏
  • 举报
回复
典型的面向过程代码。

没有“单元”可言,没办法做单元测试。
一头头 2010-05-07
  • 打赏
  • 举报
回复
黑盒测测算了吧 这么简单个东西也不用junit啊
shine333 2010-05-07
  • 打赏
  • 举报
回复
你的耦合度太高了,没法做Unit测试,无论用不用JUnit
IceArmour 2010-05-07
  • 打赏
  • 举报
回复
刚学junit的路过
amdgaming 2010-05-07
  • 打赏
  • 举报
回复
单元测试没那么难吧,assertEuqals 用用啥 ,没那么复杂吧,
  • 打赏
  • 举报
回复
四天了,居然回复数这么少,高手请进啊,非常感谢
  • 打赏
  • 举报
回复
高手请进,帮我把这个大难题解决了,分全部给他,多谢
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xblue3 的回复:]
junit的单元测试
把业务逻辑的计算方法测试一下就好了.
界面UI不用测试.
[/Quote]

具体如何测试呢?
测试对象(计算器功能介绍,把部分代码贴出来。)
测试需求(到底测试哪些,比如测试它的加减乘除 求平方根等)
测试用例设计(表格形式给出,详细介绍一个,比如加法,其他的只要直接给出用例就好)
测试代码(比如加法如何测试,代码是啥?)
测试执行
测试结果分析
meadking 2010-05-03
  • 打赏
  • 举报
回复
junit的单元测试
把业务逻辑的计算方法测试一下就好了.
界面UI不用测试.
zfq642773391 2010-05-03
  • 打赏
  • 举报
回复
帮顶 我测试下
  • 打赏
  • 举报
回复

//开根号运算
if (temp == buttonSqrt && clickable == true) {
String s = textAnswer.getText();
if (s.charAt(0) == '-') {
textAnswer.setText("负数不能开根号");
clickable = false;
}
else
textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.
parseDouble(textAnswer.getText()))));
}
//倒数运算
if (temp == buttonDao && clickable == true) {
if (textAnswer.getText().charAt(0) == '0' &&
textAnswer.getText().length() == 1) {
textAnswer.setText("零不能求倒数");
clickable = false;
}
else {
boolean isDec = true;
int i, j, k;
String s = Double.toString(1 / Double.parseDouble(textAnswer.getText()));
for (i = 0; i < s.length(); i++)
if (s.charAt(i) == '.')
break;
for (j = i + 1; j < s.length(); j++)
if (s.charAt(j) != '0') {
isDec = false;
break;
}
if (isDec == true) {
String stemp = "";
for (k = 0; k < i; k++)
stemp += s.charAt(k);
textAnswer.setText(stemp);
}
else
textAnswer.setText(s);
}
}
//按下'+/-'按钮时处理
if (temp == buttonAddAndSub && clickable == true) {
boolean isNumber = true;
String s = textAnswer.getText();
for (int i = 0; i < s.length(); i++)
if (! (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.' ||
s.charAt(i) == '-')) {
isNumber = false;
break;
}
if (isNumber == true) {
//如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
if (s.charAt(0) == '-') {
textAnswer.setText("");
for (int i = 1; i < s.length(); i++) {
char a = s.charAt(i);
textAnswer.setText(textAnswer.getText() + a);
}
}
//如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
else
textAnswer.setText('-' + s);
}
}
//计算器有关内存操作
//'MC'的操作,将内存清0
if (temp == buttonMC && clickable == true) {
memoryd = memoryi = 0;
textMemory.setText("");
}
//'MS'的操作,将当前文本框内容保存入内存,显示'M'
if (temp == buttonMS && clickable == true) {
boolean isDot = false;
textMemory.setText(" M");
for (int i = 0; i < textAnswer.getText().length(); i++)
if ('.' == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
//如果是double,则存入memoryd(double存储器)
if (isDot == true) {
memoryd = Double.parseDouble(textAnswer.getText());
memoryi = 0; //保证存储器中存放最新的值
}
//如果是int,则存入memoryi(int存储器)
else {
memoryi = Integer.parseInt(textAnswer.getText());
memoryd = 0; //保证存储器中存放最新的值
}
}
//'MR'的操作,将存储器中的信息输出
if (temp == buttonMR && clickable == true) {
if (memoryd != 0)
textAnswer.setText(Double.toString(memoryd));
if (memoryi != 0)
textAnswer.setText(Integer.toString(memoryi));
}
//'M+'的功能,将当前文本框里的数据和存储器中数据相加后,再存入存储器
if (temp == buttonMAdd && clickable == true) {
boolean isDot = false;
for (int i = 0; i < textAnswer.getText().length(); i++)
if ('.' == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
if (memoryi != 0) { //存储中是一个int型数
if (isDot == false) //被加数是一个int型数
memoryi += Integer.parseInt(textAnswer.getText());
else { //被加数是一个double型数,则将int存储器中数传入double存储器与当前数相加,int存储器清零
memoryd = memoryi + Double.parseDouble(textAnswer.getText());
memoryi = 0;
}
}
else
memoryd += Double.parseDouble(textAnswer.getText());
}
//按下'Backspace'键,利用循环将当前字符串中的最后一个字母删除
if (temp == buttonBk && clickable == true) {
String s = textAnswer.getText();
textAnswer.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
textAnswer.setText(textAnswer.getText() + a);
}
}
//按下'CE'按钮,将当前文本框内数据清除
if (temp == buttonCe) {
textAnswer.setText("");
clickable = true;
}
//按下'C'按钮,文本框内数据清除,同时var,answer清0
if (temp == buttonC) {
vard = answerd = 0;
textAnswer.setText("");
clickable = true;
}


if (temp == sItem) {
JOptionPane.showMessageDialog(panel, "当前是标准型计算器,\n科学型计算器有待更新。");
}
//按下'帮助主题'菜单栏
if (temp == topHelp) {
JOptionPane.showMessageDialog(panel, scrollHelp);
}

//按下'关于'菜单栏
if (temp == aboutCal) {
JOptionPane.showMessageDialog(panel, "开发者:zxf");
}
}
//输入中如果有操作非法,比如按下两次'+',捕获异常
catch (Exception e) {
textAnswer.setText("操作非法");
clickable = false;
}
}
//主函数
public static void main(String args[]) {
new Calculator();
}
}


加载更多回复(1)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧