【笔记】Eclipse and Java for Total Beginners—007

Lesson 07 – Create Book Class

  • Create BookTest before creating Book class
  • Test Book constructor
  • Create get and set methods

1. 新建一个JUnit test case, name为BookTest, Class under test为空,因为尚未建Book class.

2. 建立Book class, 注意目录为src。

3. Test-first approach to Book class

  • Create BookTest class.
  • Create testBook() method to test constructor.
  • Used Eclipse Quick Fix to correct compiler errors.
  • Coded constructor in Book class.
  • Ran and passed JUnit test.
1 package org.totalbeginner.tutorial;
2
3  import junit.framework.TestCase;
4
5  public class BookTest extends TestCase {
6
7 public void testBook() {
8 Book b1 = new Book("Great Expectations");
9 assertEquals("Great Expectations", b1.title);
10 assertEquals("unknown author", b1.author);
11 }
12
13 }
1 package org.totalbeginner.tutorial;
2
3  public class Book {
4
5 public String title;
6 public String author;
7
8 public Book(String string) {
9 this.title = string;
10 this.author = "unknown author";
11 }
12
13 public String getAuthor() {
14 return author;
15 }
16
17 public void setAuthor(String author) {
18 this.author = author;
19 }
20
21 public String getTitle() {
22 return title;
23 }
24
25 }
原文地址:https://www.cnblogs.com/halflife/p/2077778.html