zoukankan      html  css  js  c++  java
  • 【笔记】Eclipse and Java for Total Beginners—008

    Lesson 08 – Add Person to Book Class

    • Create a relationship between the Book class and the Person class
    • Test getPerson method
    • Create JUnit Test Suite

    1. 本课的任务

    • 目前,已创建Person class 和 Book class。
    • 需要显示哪个人借了哪本书。
    • 要创建Book 和Person之间的关联。
    • 仍用test-first方法。

    2. 步骤

         打开BookTest,添加method.

    • 创建testGetPerson() method.
    • 创建getPerson() method.
    • Run and test.

         当然,这里还是用quick fix创建相应的method。在代码里,注意Access Modifiers

    • public = access from any class
    • private = access only from this class
    • no modifier = access only from this package

         完成这步后,

    • Book class and Person class are related.
    • Book class depends on Person class.
    • One-to-one relationship ( 1 book to 1 person ) .

    3. 新招

        1> 给多行加/取消注释//,用Ctrl+/。

        2> JUnit Test Suite

        BookTest代码里有2个test,可用JUnit Test Suite达到一键测试的目的。创建步骤:

        RC package / New / Other / JUnit / JUnit Test Suite / Next …

    4. 小结

    • 创建Book和Person class之间的关联。
    • 创建这个应用程序的一键测试。

    1 package org.totalbeginner.tutorial;
    2
    3  import org.totoalbeginner.tutorial.Person;
    4
    5  import junit.framework.TestCase;
    6
    7  public class BookTest extends TestCase {
    8
    9 public void testBook() {
    10 Book b1 = new Book("Great Expectations");
    11 assertEquals("Great Expectations", b1.title);
    12 assertEquals("unknown author", b1.author);
    13 }
    14
    15 public void testGetPerson() {
    16 Book b2 = new Book("War and Peace");
    17 Person p2 = new Person();
    18 p2.setName("Elvis");
    19
    20 // method to say book is loaded to this person
    21   b2.setPerson(p2);
    22
    23 // get the name of the person who has the book
    24  // Person testPerson = b2.getPerson();
    25  // String testName = testPerson.getName();
    26  
    27 String testName = b2.getPerson().getName();
    28 assertEquals("Elvis", testName);
    29 }
    30 }

    1 package org.totalbeginner.tutorial;
    2
    3 import org.totoalbeginner.tutorial.Person;
    4
    5 public class Book {
    6
    7 String title;
    8 String author;
    9 Person person;
    10
    11 public Book(String string) {
    12 this.title = string;
    13 this.author = "unknown author";
    14 }
    15
    16 public String getAuthor() {
    17 return author;
    18 }
    19
    20 public void setAuthor(String author) {
    21 this.author = author;
    22 }
    23
    24 public String getTitle() {
    25 return title;
    26 }
    27
    28 public void setPerson(Person p2) {
    29 this.person = p2;
    30
    31 }
    32
    33 public Person getPerson() {
    34
    35 return this.person;
    36 }
    37
    38 }

    1 package org.totalbeginner.tutorial;
    2
    3 import junit.framework.Test;
    4 import junit.framework.TestSuite;
    5
    6 public class AllTests {
    7
    8 public static Test suite() {
    9 TestSuite suite = new TestSuite(AllTests.class.getName());
    10 //$JUnit-BEGIN$
    11 suite.addTestSuite(BookTest.class);
    12 suite.addTestSuite(PersonTest.class);
    13 //$JUnit-END$
    14 return suite;
    15 }
    16
    17 }

  • 相关阅读:
    用Html+Js实现的“自动补全”功能
    利用js为table添加行
    Flex 当鼠标悬停在DataGrid某行上时用datatoolField显示当前行
    Flex中设置编译器参数
    Linux2 在Linux(CentOS)上配置SSH免登陆
    线程笔记
    I/O
    网络编程
    Linux基础
    进程间的通信
  • 原文地址:https://www.cnblogs.com/halflife/p/2078326.html
Copyright © 2011-2022 走看看