- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在为策略设计模式编写单元测试。我正在将系统输出与 assertEquals 方法中的字符串进行比较。输出看起来相同,但我的测试一直失败......。我在想我忘记了与新行或制表符有关的事情?
我的单元测试:
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MiniDuck1Test {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
@Test
public void testDuck1() {
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly();
Duck model = new ModelDuck();
model.performFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performFly();
assertEquals("Quack\nI'm flying!!\nI can't fly\nI'm flying with a rocket", outContent.toString().trim());
}
}
输出(第二行和第三行显示为红色):
Quack
I'm flying!!
I can't fly
I'm flying with a rocket
编辑:
最快的解决方案似乎是在我的“\n”前面添加一个“\r”。多个答案告诉我这需要为 Windows 完成。应用这个之后我的 assertEquals 看起来像:
assertEquals("Quack\r\nI'm flying!!\r\nI can't fly\r\nI'm flying with a rocket", outContent.toString().trim());
另外:我忘了说大部分代码都来自 Eric Freeman、Elisabeth Robson、Bert Bates 和 Kathy Sierra 合着的“Head First Design Patterns”一书。
最佳答案
除了其他答案之外,如果您正在寻找一种独立于平台的方式...
一个快速的独立于平台的解决方案可以是替换行分隔符
String expected = "Quack\nI'm flying!!\nI can't fly\nI'm flying with a rocket"
.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"));
assertEquals(expected, outContent.toString().trim());
或使用 PrintWriter
构建预期的字符串。
StringWriter expectedStringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
printWriter.println("Quack");
printWriter.println("I'm flying!!");
printWriter.println("I can't fly");
printWriter.println("I'm flying with a rocket");
printWriter.close();
String expected = expectedStringWriter.toString();
assertEquals(expected, outContent.toString());
或者创建一个自己的断言类来重用它
class MyAssert {
public static void assertLinesEqual(String expectedString, String actualString){
BufferedReader expectedLinesReader = new BufferedReader(new StringReader(expectedString));
BufferedReader actualLinesReader = new BufferedReader(new StringReader(actualString));
try {
int lineNumber = 0;
String actualLine;
while((actualLine = actualLinesReader.readLine()) != null){
String expectedLine = expectedLinesReader.readLine();
Assert.assertEquals("Line " + lineNumber, expectedLine, actualLine);
lineNumber++;
}
if(expectedLinesReader.readLine() != null){
Assert.fail("Actual string does not contain all expected lines");
}
} catch (IOException e) {
Assert.fail(e.getMessage());
} finally {
try {
expectedLinesReader.close();
} catch (IOException e) {
Assert.fail(e.getMessage());
}
try {
actualLinesReader.close();
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
}
}
然后如果测试失败你可以给出更好的问题描述。例如
MyAssert.assertLinesEqual(
"Quack\nI'm flying!!\nI can not fly\nI'm flying with a rocket\n",
outContent.toString());
会输出
org.junit.ComparisonFailure: Line 2
Expected :I can not fly
Actual :I can't fly
关于Java:使用 assertEquals 测试系统输出,包括 "new lines",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674408/
我创建了一个方法来计算字符串中给定字符的出现次数。 public Integer numberOf(String str, Character a){} 我试着像往常一样测试: @Test publi
我有一个 junit 测试断言两个 Double 对象,具有以下内容: Assert.assertEquals(Double expected, Double result); 这很好,然后我决定将其
我收到以下错误: Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert matc
我是 Mockito 的新手,需要为工作学习它。 我做了一个非常简单的类,它有一个返回字符串的方法。 然后我在eclipse中做了下面的测试类。 import static org.junit.Ass
这个问题在这里已经有了答案: differences between 2 JUnit Assert classes (6 个回答) 7年前关闭。 我用 assertEquals(String, Str
这是一个 PHPUnit 测试: class EqualsTest extends PHPUnit_Framework_TestCase { public function testSucces
所以我正在学习如何在 Django 中练习 TDD,但遇到了一些小问题。我创建了一个自定义用户对象,它以一对一的关系链接到经过身份验证的系统用户。我有以下测试,它练习了我的自定义用户类的一部分: de
任何人都可以帮助我使用 AssertEquals 吗? 我的测试用例类有以下代码,但在 AssertEquals 失败后,测试继续进行下一个方法,即 createClientTodelete。为什么?
我的目标是确保对象图具有预期的值和类型。我想确保每个值都是预期的类型。 为此,assertEquals() 不幸的是没有用: $this->assertEquals( [ 'prop' =>
boolean b = Testfactory.read_element_file(models).equals(ExcelUtils.readdata("Model Export.xlsx"))
我有一个字符串-数字对的类。该类实现了 compareTo 方法。 另一个类的方法返回pair类型的元素集合。 我想对此方法执行单元测试,因此编写了以下内容: @Test public void te
我试图比较两个对象,首先比较它们是否具有相同的键,然后比较它们的键值是否相等,但来自assertEquals的测试仍然失败。我认为这是因为这两个对象位于一个数组内部,所以它实际上是在比较一个对象数组与
即使两个字符串相同,assertEquals() 也会失败...有人能帮我弄清楚为什么吗? public void testSet() { ByteArrayOutputStream ou
我正在通过 JUnit 进行 TDD 实践来开发我的应用程序。我需要确保传递给该方法的参数存在于类字典(Hashmap)中。为此,我使用assertEquals,比较模拟字典对象和方法。然而,IDEA
当使用 assertEquals() 比较两个对象时,是否考虑查看内部结构,即。对象的属性? 假设我有一个类 A,如下所示: public class A { private int ID;
我正在尝试使用 Junit 测试用例为 Course 类中的 GetTeextBook() 创建一个测试方法。请注意 GetTextBook() 是 Textbook 对象的类型,但是当我在 test
我实际上是想了解 PHPUNIT 和使用 PHP 进行单元测试。 我有一个经典的划分方法。 public function divide($firstNumber, $secondNumber){
我正在尝试使用 HUnit 以 TDD 方式在 Real World Haskell 中进行练习.正如您可能猜到的那样,我还没有走得太远,所以就 Haskell 而言,我是一个绝对的初学者。给定以下代
当我尝试在我的代码中使用以下语句时,获取对象没有属性“assertEqual”: self.assertEqual("IRELAND INSTITUTE OF PITTSBURGH", driver.
我有两套: Set set1 = new HashSet(5); Set set2 = new HashSet(5); //add 5 attribute objects to each of
我是一名优秀的程序员,十分优秀!