Junit - 测试不同的 class
Junit - testing a different class
您好,我正在尝试进行 Junit 测试,但我无法找到一种方法来测试另一个 class 而不复制粘贴它的部分内容。说我想测试这个:
import java.io.*;
public class Calculator {
public static void main(String[] args) {
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
int opInt = Integer.parseInt(op);
double result = 0;
switch (opInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
return;
}
System.out.println("The answer is " + result);
}
private static double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
private static double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 * d2;
return result;
}
private static double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
private static double addValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
有什么方法可以设置 JUnit 案例测试来检查其中的某些部分,而无需为每个测试复制和粘贴它或修改原始 class。我是不是遗漏了什么或者这是 Junit 做不到的?
这是我目前的情况:
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.junit.Test;
public class CalculatorTest {
Calculator mycalculator = new Calculator();
@Test
public void test1( ) {
mycalculator;
assertEquals(d1 + d2, 20);
}
}
在 JUnit 测试中,您可以在测试文件中包含 classes。无需从 class 复制和粘贴代码并将其放入测试文件中。它看起来更像:
计算器 c = new 计算器();
c.myFunction();
您也可以将断言语句添加到您的测试函数中,以确认您从函数调用中获得了正确的结果。
我只使用 Eclipse 完成了 JUnit 测试,但实际上您创建了一个新的 JUnit 文件(就像您创建 Class 一样),它会自动设置文件的基本结构。然后从那里您可以添加任何您想要的测试。您也可以导入任何您需要的 classes。
- 释放所有静态方法。
- 在您的主要创建计算器实例和运行计算方法中。
- 现在您可以使用 JUnit 测试 Calcultor 计算方法
限制包的范围将使您能够测试此 class,如果测试 class 将在同一个包中(但在测试源中)
public class Calculator {
public static void main(String[] args) {
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
new Calculator().calculate(s1, s2, op);
}
public void calculate(String s1, String s2, String op)
int opInt = Integer.parseInt(op);
double result = 0;
switch (opInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
return;
}
System.out.println("The answer is " + result);
}
double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 * d2;
return result;
}
double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
double addValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
您 class 的设计并不真正适合自动化测试。
- class 中唯一的方法是私有静态的,这意味着它们只能从这个 class 中的其他静态方法访问(尽管可以使用反射来克服这个问题,如果你绝对必须有私有静态成员。)
- 部分 class 需要用户输入/干预,这使得自动测试它们变得困难。
- 您的 class 不是面向对象的。它写得更像一个函数式程序(例如C),有一个主体和全局函数,而不是写成一个提供功能的对象。
试试这样的方法:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Calculator {
// TODO: Move enum to another file
public static enum OperatorType {
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE
}
public double calculateResult(double operand1, double operand2, OperatorType operator) {
double result = 0;;
switch (operator) {
case ADD:
result = addValues(operand1, operand2);
break;
case DIVIDE:
result = subtractValues(operand1, operand2);
break;
case MULTIPLY:
result = multiplyValues(operand1, operand2);
break;
case SUBTRACT:
result = subtractValues(operand1, operand2);
break;
default:
break;
}
return result;
}
public double divideValues(double d1, double d2) {
double result;
if (d2 != 0) {
result = d1 / d2;
} else {
// Avoid divide-by-zero error (could also throw it if preferred)
result = 0;
}
return result;
}
public double multiplyValues(double d1, double d2) {
double result = d1 * d2;
return result;
}
public double subtractValues(double d1, double d2) {
double result = d1 - d2;
return result;
}
public double addValues(double d1, double d2) {
double result = d1 + d2;
return result;
}
public static void main(String[] args) {
// Get and validate user input
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
// TODO: Handle NumberFormatExceptions here
double operand1 = Double.parseDouble(s1);
double operand2 = Double.parseDouble(s2);
OperatorType operator;
int opInt = Integer.parseInt(op);
switch (opInt) {
case 1:
operator = OperatorType.ADD;
break;
case 2:
operator = OperatorType.SUBTRACT;
break;
case 3:
operator = OperatorType.MULTIPLY;
break;
case 4:
operator = OperatorType.DIVIDE;
break;
default:
System.out.println("You entered an incorrect value");
return;
}
// Use class to calculate result
Calculator calculator = new Calculator();
double result = calculator.calculateResult(operand1, operand2, operator);
// Output results
System.out.println("The answer is " + result);
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
}
- 你所有的个人数学运算都是public计算器class的方法,可以单独测试。
- 主要的数学逻辑,需要两个操作数和一个运算符,在另一个public方法中,也可以测试
- 用户输入和输出保留在 main 方法中,因为它是您要通过自动化测试测试的逻辑(而不是用户 input/output)。
- 输入的所有类型转换都保留在 main 方法中。您的方法应该对正确的数据类型进行操作,而不是将字符串作为输入然后尝试解析它们。在 main 方法中保留解析(和解析的错误处理)。
您好,我正在尝试进行 Junit 测试,但我无法找到一种方法来测试另一个 class 而不复制粘贴它的部分内容。说我想测试这个:
import java.io.*;
public class Calculator {
public static void main(String[] args) {
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
int opInt = Integer.parseInt(op);
double result = 0;
switch (opInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
return;
}
System.out.println("The answer is " + result);
}
private static double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
private static double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 * d2;
return result;
}
private static double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
private static double addValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
有什么方法可以设置 JUnit 案例测试来检查其中的某些部分,而无需为每个测试复制和粘贴它或修改原始 class。我是不是遗漏了什么或者这是 Junit 做不到的?
这是我目前的情况:
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.junit.Test;
public class CalculatorTest {
Calculator mycalculator = new Calculator();
@Test
public void test1( ) {
mycalculator;
assertEquals(d1 + d2, 20);
}
}
在 JUnit 测试中,您可以在测试文件中包含 classes。无需从 class 复制和粘贴代码并将其放入测试文件中。它看起来更像:
计算器 c = new 计算器();
c.myFunction();
您也可以将断言语句添加到您的测试函数中,以确认您从函数调用中获得了正确的结果。
我只使用 Eclipse 完成了 JUnit 测试,但实际上您创建了一个新的 JUnit 文件(就像您创建 Class 一样),它会自动设置文件的基本结构。然后从那里您可以添加任何您想要的测试。您也可以导入任何您需要的 classes。
- 释放所有静态方法。
- 在您的主要创建计算器实例和运行计算方法中。
- 现在您可以使用 JUnit 测试 Calcultor 计算方法
限制包的范围将使您能够测试此 class,如果测试 class 将在同一个包中(但在测试源中)
public class Calculator {
public static void main(String[] args) {
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
new Calculator().calculate(s1, s2, op);
}
public void calculate(String s1, String s2, String op)
int opInt = Integer.parseInt(op);
double result = 0;
switch (opInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
return;
}
System.out.println("The answer is " + result);
}
double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 * d2;
return result;
}
double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
double addValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
您 class 的设计并不真正适合自动化测试。
- class 中唯一的方法是私有静态的,这意味着它们只能从这个 class 中的其他静态方法访问(尽管可以使用反射来克服这个问题,如果你绝对必须有私有静态成员。)
- 部分 class 需要用户输入/干预,这使得自动测试它们变得困难。
- 您的 class 不是面向对象的。它写得更像一个函数式程序(例如C),有一个主体和全局函数,而不是写成一个提供功能的对象。
试试这样的方法:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Calculator {
// TODO: Move enum to another file
public static enum OperatorType {
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE
}
public double calculateResult(double operand1, double operand2, OperatorType operator) {
double result = 0;;
switch (operator) {
case ADD:
result = addValues(operand1, operand2);
break;
case DIVIDE:
result = subtractValues(operand1, operand2);
break;
case MULTIPLY:
result = multiplyValues(operand1, operand2);
break;
case SUBTRACT:
result = subtractValues(operand1, operand2);
break;
default:
break;
}
return result;
}
public double divideValues(double d1, double d2) {
double result;
if (d2 != 0) {
result = d1 / d2;
} else {
// Avoid divide-by-zero error (could also throw it if preferred)
result = 0;
}
return result;
}
public double multiplyValues(double d1, double d2) {
double result = d1 * d2;
return result;
}
public double subtractValues(double d1, double d2) {
double result = d1 - d2;
return result;
}
public double addValues(double d1, double d2) {
double result = d1 + d2;
return result;
}
public static void main(String[] args) {
// Get and validate user input
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
String op = getInput("Enter 1=ADD, 2=Subtract, 3=Multiply, 4=Divide ");
// TODO: Handle NumberFormatExceptions here
double operand1 = Double.parseDouble(s1);
double operand2 = Double.parseDouble(s2);
OperatorType operator;
int opInt = Integer.parseInt(op);
switch (opInt) {
case 1:
operator = OperatorType.ADD;
break;
case 2:
operator = OperatorType.SUBTRACT;
break;
case 3:
operator = OperatorType.MULTIPLY;
break;
case 4:
operator = OperatorType.DIVIDE;
break;
default:
System.out.println("You entered an incorrect value");
return;
}
// Use class to calculate result
Calculator calculator = new Calculator();
double result = calculator.calculateResult(operand1, operand2, operator);
// Output results
System.out.println("The answer is " + result);
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "error: " + e.getMessage();
}
}
}
- 你所有的个人数学运算都是public计算器class的方法,可以单独测试。
- 主要的数学逻辑,需要两个操作数和一个运算符,在另一个public方法中,也可以测试
- 用户输入和输出保留在 main 方法中,因为它是您要通过自动化测试测试的逻辑(而不是用户 input/output)。
- 输入的所有类型转换都保留在 main 方法中。您的方法应该对正确的数据类型进行操作,而不是将字符串作为输入然后尝试解析它们。在 main 方法中保留解析(和解析的错误处理)。