尝试编写 Junit 测试,但失败了
Trying to write a Junit test, but it's failing
这是我的代码:
Concatenate.java
package concatenate;
import java.util.Scanner;
public class Concatenate {
private static String firstWord;
private static String secondWord;
public Concatenate(String firstWord, String secondWord) {
Concatenate.firstWord = firstWord;
Concatenate.secondWord = secondWord;
}
public String getFirst() {
return firstWord;
}
public String getSecond() {
return secondWord;
}
public static Concatenate something() {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter first word");
firstWord = myObj.nextLine();
// Scanner newObj = new Scanner(System.in);
System.out.println("Enter second word");
secondWord = myObj.nextLine();
return new Concatenate(firstWord, secondWord);
}
public static void main(String[] args) {
Concatenate result = something();
System.out.println(result.getFirst() + " " + result.getSecond());
}
}
ConcatenateTest.java
package concatenate;
import org.junit.Test;
import static org.junit.Assert.*;
public class ConcatenateTest {
public ConcatenateTest() {
}
@Test
public void testMain() {
String firstWord = "Hello";
String secondWord = "world";
String expectedResult = "Hello world";
Concatenate testResult = new Concatenate(firstWord, secondWord);
assertEquals(expectedResult, testResult);
assertEquals(1, 1);
}
}
但是当我 运行 测试时,它失败了,我得到...
Expected <hello world> but was concatenate.Concatenate@[alphanumeric string]
有什么指点吗?
顺便说一句,我试图将所有这些复制到 Eclipse,但它引发了一大堆额外的错误。如果有人能告诉我为什么那也很好。 https://i.imgur.com/iES6owx.jpg
TIA
测试失败,因为 expectedResult 是一个字符串,而 testResult 是一个 Concatenate 的实例。他们不相等。
"Hello world".equals(testResult) == false
因为您正在比较 String 和 Concatenate 对象。所以它不会匹配。
最好在 Concatenate class 中创建一个 concat 方法并对方法的输出断言
public String concat() {
return firstWord + " " + secondWord;}
并比较
assertEquals(expectedResult, testResult.concat());
希望这能澄清您的问题
"I understand that, thanks, but what's the solution then please? Because when I try to make my Concatenate function return a String I get the error missing return statement"
你在谈论你的构造函数吗?如果是 它不能有 return。
您可以覆盖 equals 方法然后更改您的测试
@Override
public String toString() {
return firstWord +" "+secondWord;
}
assertEquals(expectedResult, testResult.toString());
这是我的代码:
Concatenate.java
package concatenate;
import java.util.Scanner;
public class Concatenate {
private static String firstWord;
private static String secondWord;
public Concatenate(String firstWord, String secondWord) {
Concatenate.firstWord = firstWord;
Concatenate.secondWord = secondWord;
}
public String getFirst() {
return firstWord;
}
public String getSecond() {
return secondWord;
}
public static Concatenate something() {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter first word");
firstWord = myObj.nextLine();
// Scanner newObj = new Scanner(System.in);
System.out.println("Enter second word");
secondWord = myObj.nextLine();
return new Concatenate(firstWord, secondWord);
}
public static void main(String[] args) {
Concatenate result = something();
System.out.println(result.getFirst() + " " + result.getSecond());
}
}
ConcatenateTest.java
package concatenate;
import org.junit.Test;
import static org.junit.Assert.*;
public class ConcatenateTest {
public ConcatenateTest() {
}
@Test
public void testMain() {
String firstWord = "Hello";
String secondWord = "world";
String expectedResult = "Hello world";
Concatenate testResult = new Concatenate(firstWord, secondWord);
assertEquals(expectedResult, testResult);
assertEquals(1, 1);
}
}
但是当我 运行 测试时,它失败了,我得到...
Expected <hello world> but was concatenate.Concatenate@[alphanumeric string]
有什么指点吗?
顺便说一句,我试图将所有这些复制到 Eclipse,但它引发了一大堆额外的错误。如果有人能告诉我为什么那也很好。 https://i.imgur.com/iES6owx.jpg
TIA
测试失败,因为 expectedResult 是一个字符串,而 testResult 是一个 Concatenate 的实例。他们不相等。
"Hello world".equals(testResult) == false
因为您正在比较 String 和 Concatenate 对象。所以它不会匹配。
最好在 Concatenate class 中创建一个 concat 方法并对方法的输出断言
public String concat() {
return firstWord + " " + secondWord;}
并比较
assertEquals(expectedResult, testResult.concat());
希望这能澄清您的问题
"I understand that, thanks, but what's the solution then please? Because when I try to make my Concatenate function return a String I get the error missing return statement"
你在谈论你的构造函数吗?如果是 它不能有 return。 您可以覆盖 equals 方法然后更改您的测试@Override
public String toString() {
return firstWord +" "+secondWord;
}
assertEquals(expectedResult, testResult.toString());