[英]How to Test a Queue in JUnit 5?
I am struggling to implement test cases on this program.我正在努力在这个程序上实施测试用例。 The program takes in a queue and sorts it in ascending order.
该程序接收一个队列并按升序对其进行排序。 It's working fine.
它工作正常。 However, when I try to test it's returned result using JUnit 5, I get an error.
但是,当我尝试使用 JUnit 5 测试它返回的结果时,出现错误。 I am putting the expected result and the actual result in the testing method of the testing class.
我将预期结果和实际结果放在测试 class 的测试方法中。 It still does not work.
它仍然不起作用。 Please help!
请帮忙!
(Edit: Testing methods) (编辑:测试方法)
Here's the code:这是代码:
import java.util.Queue;
public class Sort {
public static Queue<Integer> queueSort(Queue<Integer> queue) {
int n = queue.size();
if (queue.isEmpty()) {
System.out.println("Invalid Input");
} else {
for (int i = 0; i < n; i++) {
int minimumIndex = -1;
int minimumValue = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
int currentValue = queue.poll();
if (currentValue < minimumValue && j < (n - i)) {
minimumValue = currentValue;
minimumIndex = j;
}
queue.add(currentValue);
}
for (int j = 0; j < n; j++) {
int currentValue = queue.poll();
if (j != minimumIndex) {
queue.add(currentValue);
}
}
queue.add(minimumValue);
}
}
return queue;
}
}
The test class:测试 class:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.Queue;
class SortTest {
Queue<Integer> Q1 = new LinkedList<>();
Queue<Integer> expectedQ1 = new LinkedList<>();
Queue<Integer> Q2 = new LinkedList<>();
Queue<Integer> expectedQ2 = new LinkedList<>();
Queue<Integer> Q3 = new LinkedList<>();
void testInputs() {
Q1.add(10); Q1.add(-1); Q1.add(8); Q1.add(7); Q1.add(2);
expectedQ1.add(-1); expectedQ1.add(2); expectedQ1.add(7); expectedQ1.add(8); expectedQ1.add(10);
Q2.add(6000); Q2.add(5999);
expectedQ2.add(5999); expectedQ2.add(6000);
}
@Test
void queueSortTest1() {
Assertions.assertEquals(expectedQ1, Sort.queueSort(Q1));
}
@Test
void queueSortTest2() {
Assertions.assertEquals(expectedQ2, Sort.queueSort(Q2));
}
@Test
void queueSortTest3() {
Assertions.assertEquals("Invalid Input", Sort.queueSort(Q3));
}
}
You should use org.junit.Assert.assertEquals for Queue equality check.您应该使用 org.junit.Assert.assertEquals 进行队列相等性检查。
If you want to consider the order in your equality check, you can do such trick:如果你想在你的平等检查中考虑顺序,你可以这样做:
Assertions.assertTrue(Arrays.equals(q1.toArray(), queueSort(q1).toArray()));
To make this code more beautiful, you can implement a custom matcher (for example, using hamcrest ).为了使这段代码更漂亮,您可以实现一个自定义匹配器(例如,使用hamcrest )。
Pay your attention, that your code is changing an initial queue (looks like a bug:) ).请注意,您的代码正在更改初始队列(看起来像一个错误:))。
So所以
Queue<Integer> sortedq1 = Sort.queueSort(q1);
Assertions.assertTrue(Arrays.equals(q1.toArray(), sortedq1.toArray()));
will always pass (as q1 was already sorted).This post can help to understand the reason.将始终通过(因为 q1 已经排序)。这篇文章可以帮助理解原因。
I see that you edited the question.我看到你编辑了这个问题。 So adding the System.out validation as well:
因此,还要添加 System.out 验证:
@Test
void queueSort() {
final PrintStream standardOut = System.out;
final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStreamCaptor));
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> sortedq1 = Sort.queueSort(q1);
Assertions.assertEquals("Invalid Input", outputStreamCaptor.toString().trim());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.