Java 使用IsEmpty()测试堆栈是否为空/满(&;isFull()JUnit测试
我试图编写一个isEmpty()和一个isFull()方法,检查堆栈是否为空或其中是否有值。我不确定如何编写方法,以便所有测试都以成功的形式返回。下面是类和方法的代码:Java 使用IsEmpty()测试堆栈是否为空/满(&;isFull()JUnit测试,java,stack,junit5,Java,Stack,Junit5,我试图编写一个isEmpty()和一个isFull()方法,检查堆栈是否为空或其中是否有值。我不确定如何编写方法,以便所有测试都以成功的形式返回。下面是类和方法的代码: public class stack { private int top; private int maxSize; private String stackItems[]; //default no-args constructor public stack(
public class stack {
private int top;
private int maxSize;
private String stackItems[];
//default no-args constructor
public stack() {
this.maxSize = 5;
this.top = -1;
this.stackItems = new String[maxSize];
}
//allows you to set the max size of the stack
public stack(int maxSize) {
this.maxSize = maxSize;
this.top = -1;
this.stackItems = new String[maxSize];
}
//I have two tests for this method: testIsFullFalse and testIsFullTrue
public boolean isFull() {
return top == maxSize -1;
}
//Two tests for this method: testIsEmptyFalse and testIsEmptyTrue
public boolean isEmpty() {
// In progress
return top != maxSize -1;
}
以下是测试:
@Test
void testIsEmptyTrue() {
// ARRANGE
stack myStack = new stack(1);
boolean actual, expected;
// ACT
actual = myStack.isEmpty();
expected = true;
// ASSERT
assertEquals(expected, actual);
}
@Test
void testIsEmptyFalse() throws StackFullException {
// ARRANGE
stack myStack = new stack(1);
String item = "Java is Fun!";
boolean actual, expected;
// ACT
myStack.push(item);
actual = myStack.isEmpty();
expected = false;
// ASSERT
assertEquals(expected, actual);
}
@Test
void testIsFullTrue() throws StackFullException {
// ARRANGE
stack myStack = new stack(1);
String item = "testing";
boolean actual, expected;
// ACT
myStack.push(item);
actual = myStack.isFull();
expected = true;
// ASSERT
assertEquals(expected, actual);
}
@Test
void testIsFullFalse() throws StackFullException {
// ARRANGE
stack myStack = new stack(1);
boolean actual, expected;
// ACT
actual = myStack.isFull();
expected = false;
// ASSERT
assertEquals(expected, actual);
}
现在,testismptytrue()和testIsFullFalse()返回为成功,testismptyfalse()和testIsFullTrue()返回为失败。我希望他们都能成功地回来。有没有关于如何修复方法的指针?在没有看到push方法的情况下有点难,但问题可能在这里:
//Two tests for this method: testIsEmptyFalse and testIsEmptyTrue
public boolean isEmpty() {
// In progress
return top != maxSize -1;
}
isEmpty()
方法将始终返回true,除非top
等于maxSize-1
对于一个名为“isEmpty”的方法,类似这样的方法如何:
private static final int bottom = -1;
private int top = bottom;
private int maxSize;
private String stackItems[];
//default no-args constructor
public stack() {
this.maxSize = 5;
//this.top = -1;
this.stackItems = new String[maxSize];
}
//allows you to set the max size of the stack
public stack(int maxSize) {
this.maxSize = maxSize;
//this.top = -1;
this.stackItems = new String[maxSize];
}
...
//Two tests for this method: testIsEmptyFalse and testIsEmptyTrue
public boolean isEmpty() {
// In progress
return top == bottom;
}
显然,
push()
方法有问题。如果没有它,您的问题是不完整的。您的方法isEmpty()
的名称很差,因为(根据您的impl判断)它测试的不是对象是否为空,而是对象是否为空。我建议将impl与名称对齐,或者简单地将impl更改为return!isFull()代码>