运行JUnit 出现找不到测试类的问题

2024-12-04 22:34:47
推荐回答(1个)
回答1:

ass Book {
public String name;
public String author;
private int availNum;

public Book() {
this("", "", 0);
}

public Book(String name, String author, int availNum) {
this.name = name;
this.author = author;
this.availNum = availNum;
}

public int getAvailNum() {
return availNum;
}

public void setAvailNum(int availNum) {
this.availNum = availNum;
}

public boolean subscribe() {
if (availNum > 0) {
availNum--;
return true;
}
return false;
}

public boolean cancelSubscribe() {
availNum++;
return true;
}
}

/** Book类的单元测试类 */
import junit.framework.TestCase;

public class TestBook extends TestCase {

public void testBook() {
Book book = new Book("Thinking in Java", "Bruce Eckel", 3);
assertEquals(true, book.subscribe());// 第一次预借,应该是成功的
assertEquals(2, book.getAvailNum());// 此时的可预借数是2
assertEquals(true, book.cancelSubscribe());// 取消预借,应该是成功的
assertEquals(3, book.getAvailNum());// 此时的可预借数是3
book.setAvailNum(0);// 将可预借数设为0
assertEquals(false, book.subscribe());// 此时预借,应该是不成功的
}

public static void main(String[] args) {
junit.textui.TestRunner.run(Book.class);
}
}
上面的测试类可以不要main函数,直接在集成开发环境里用JUnit执行单元测试。