gpt4 book ai didi

java - 对象类型方法的 assertequal

转载 作者:行者123 更新时间:2023-11-28 21:04:46 25 4
gpt4 key购买 nike

我正在尝试使用 Junit 测试用例为 Course 类中的 GetTeextBook() 创建一个测试方法。请注意 GetTextBook()Textbook 对象的类型,但是当我在 testGetTextBook 方法中运行 CourseTestCase1 类时,我总是会失败()

有什么建议吗?

CourseTestCase1

public class CourseTestCase1 {

private Course course=new Course(101, "Software Engineering");

@Test
public void testGetTextBook() {
Textbook testbook=new Textbook(700, "Data Base System", "Ramez ELmasri",2011);
assertEquals(testbook,course.getTextbook());
}

@Test
public void testGetCourseCode() {
assertEquals(101,course.getCourseCode());
}

@Test
public void testGetName() {
assertEquals("Software Engineering",course.getName());
}

@Test
public void testToString() {
testGetCourseCode();
testGetName();
}
}

类(class)

public class Course {

private int courseCode;
private String courseName;
private Textbook textbook;

private Semester semestr;

public Course() {
this.courseCode = 0;
this.courseName = null;

this.semestr = null;
this.textbook = null;
}

public Course(int courseCode, String courseName) {
this.courseCode = courseCode;
this.courseName = courseName;

semestr = null;
textbook = null;
}

public Textbook getTextbook() {
return textbook;
}
}

教科书

public class Textbook {
private int isbn;
private String title, authors;
private int publicationYear;

private TextbookCopy tbcopy;

public Textbook() {
isbn = 0;
title = null;
authors = null;
publicationYear = 0;
}

public Textbook(int isbn, String title, String authors, int publicationYear) {
this.isbn = isbn;
this.title = title;
this.authors = authors;
this.publicationYear = publicationYear;
}

public Textbook(int isbn, String title, String authors, int publicationYear,
String publisher, String distributor, double purchasePrice) {
this.isbn = isbn;
this.title = title;
this.authors = authors;
this.publicationYear = publicationYear;
}

public String getTitle() {
return title;
}

public int getPublicationYear() {
return publicationYear;
}

public String getAuthors() {
return authors;
}

public Textbook getBook() {
return this;
}

public int getISBN() {
return isbn;
}

public String getCopy() {
return Integer.toString(tbcopy.numbersOfCopy);
}

public void USwoltextbook(int isbn) {
}

public void assignNewBook() {
}

public Textbook createNewTextBook(String info) {
return null;
}
}

最佳答案

您需要正确定义 Textbook.equals() 方法。默认实现只是检查它是否是同一个对象。在您的情况下,检查字段内容是否相同是合理的。如果您使用的是 IDE,应该有一个按钮可以自动生成这样的 equals() 方法(例如,在 Eclipse 中:Source -> Generate hashCode() and equals())。

关于java - 对象类型方法的 assertequal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com