1. Summary

  • Configure your machines to use IntelliJ and Java

  • Walk through a simple example of

    • create a class

    • create a test class

    • write tests

    • run tests

  • Practise designing simple classes

  • Practise designing classes that contain other classes

It is not required or expected that you finish all the question during lab hours. It is strongly recommended that you do finish all parts of the lab and push your lab solutions to Github. Labs are designed to get you started and get some practise while the course staff is present to provide assistance.

2. Setup

2.1. Java Tools

Download and install the Java Development Kit (JDK) version 8.

  1. Go to the section with the title "Java SE Development Kit 8u92"

  2. Click on the radio button with the title "Accept License Agreement"

  3. Click on the appropriate link based on the operating system that you have on your machine

    • For Windows select Widnows x64

    • For Macs select Mac OS X

    • For Linux selecet the appropriate item, typically Linux x64

  4. Allow the download to complete

  5. Install the newly downloaded application using the process that you typically use for your operating system to install new software

2.2. IntelliJ

The instructor will be using IntelliJ in class and labs. You do not have to install IntelliJ. We will try and help you regardless of your IDE choice but be prepared to do extra work on your own if you select an IDE other than than the one used in class.

  • IntelliJ.

    1. As a student you get the full version of the IDE for free. Apply for the student discount first.

    2. Download IntelliJ (any verion will do)

    3. Install IntelliJ on your machine using the process that you typically use for your operating system to install new software

  • Eclipse

  • NetBeans

2.3. Test your Setup

  1. Clone your git repository to a folder on your computer.

  2. Open IntelliJ

  3. From IntelliJ’s main meny select New → Project. The New Project Dialog window should appear with two tabs

    • a list of project types on the left

    • a dropdown with the title "Project SDK" on the right

    • a list of additional libraries also on the right

  4. Select Java from the list of project types on the left

  5. Using the dropdown on the right select 1.8 as your projects JDK version

  6. Click Next. The contents of the window will change

  7. Select nothing in this window and click Next. The contents of the window will change

  8. You should see two fields

    • "Project Name", provide a name for your project. This name can be anything you like.

    • "Project Location", this is the location on your computer that IntelliJ is going to use to create and store all your code. There should be a button to the far right of this field whose title is three dots, i.e., …​. Click this button and navigate to the folder where created due to the git clone operation in step 1. For the first lab it should be lab1-<login> where <login> is your CCIS Github login name.

  9. Click Finish

  10. IntelliJ will now open in a new window that will contain two tabs

    • A left tab that is your Project Explorer. This is similar to your file explorer

    • A right tab that will be used to show the contents of files that you select in the Project Explorer.

  11. In the Project Explorer you should see a folder with the name of the git cloned folder lab1-<login>. Double click on the folder’s name to expand it.

  12. You should see a sub-folder named src

  13. Right click on the folder named src to open the context menu. From the context menu select New → Class to create a new Java Class.

  14. A small window will pop-up asking you to provide a name for your class. Input the name Author and click OK

  15. IntelliJ will detect that you are inside a git repository and will ask you if you would like to add the file to your repo. We will not cover the IntelliJ git integration in this lab. You are free to read the IntelliJ documentation on how to use git from inside IntelliJ. Click No

  16. The right tab of your IntelliJ window should now contain a minimal Java class with

    • a Java comment as the first line

    • an empty Java class definition

  17. Notice that in the Project Explorer tab there is now a new file named Author under the folder named src

  18. To test that your setup is working as expected, replace all the contents of the file Author.java with the following code

Author.java
/**
 * Represents an Author with their details--name, email and physical address
 *
 * @author therapon
 *
 */

public class Author {

  private String name;
  private String email;
  private String address;

  /**
   * Creates a new author given the author's name, email and address as strings.
   *
   * @param name the author's name
   * @param email the author's email address
   * @param address the authors physical address
   */
  public Author(String name, String email, String address) {
    this.name = name;
    this.email = email;
    this.address = address;
  }

  /**
   * @return the name
   */
  public String getName() {
    return this.name;
  }

  /**
   * @return the email
   */
  public String getEmail() {
    return this.email;
  }

  /**
   * @return the address
   */
  public String getAddress() {
    return this.address;
  }
}
  1. IntelliJ tried to assist you while you code by popping up an image of a small light bulb inside your editor (the right tab). Move your cursor to be inside the name of class, i.e., the word Author in the class header public class Author {. Wait for a couple of seconds and the yellow light bulb image should appear at the start of that line. You can force the IntelliJ assistant to open using the following keystrokes

    • Windows/Linux :Alt+Enter

    • Mac :Option+Enter

  2. Click on the yellow light bulb icon and a menu should appear.

  3. From the menu select Create Test. This action will cause a new pop-up window to appear.

  4. In the new pop-up window we will configure and create a test for our Author class. Starting from the top of the window going down

  5. For "Testing library" select JUnit 4

  6. Click the Fix button to add JUnit4 to your project. A new pop-up window will appear.

  7. Select the first option with the title "Use JUnit4 from IntelliJ IDEA distribution"

  8. Click OK. This will close this pop-up window and takes us back to continue setting up our test for Author.

  9. Back in the pop-up for setting up our test for Author leave "Superclass" empty.

  10. Leave "Destination Package" empty

  11. Select the check mark with the title "setUp/@Before" only.

  12. At the bottom of this pop-up window you should see the list of methods that are available in class Author for testing. Select all of them.

  13. Click OK

  14. Select JUnit Test Case from the pop-up menu. This will create a new Java class (and a new file) called AuthorTest. If IntelliJ asks you to add this new file to your repo click No.

  15. The Package Explorer should now have a new file with the name AuthorTest under the folder src

  16. The editor should now display

AuthorTest.java
public class AuthorTest {
    @org.junit.Before
    public void setUp() throws Exception {

    }

    @org.junit.Test
    public void getName() throws Exception {

    }

    @org.junit.Test
    public void getEmail() throws Exception {

    }

    @org.junit.Test
    public void getAddress() throws Exception {

    }

}
  1. Notice the red lines in the right margin of your editor. Use your mouse to hover over the red lines and see the issue. The issue here is that we have not added the JUnit4 library’s code so that IntelliJ can find it. Move your cursor to the second line in the file AuthorTest that contains the string @org.junit.Before and force the IntelliJ assistant (see step 19).

  2. From the assistant’s pop-up menu select "Add junit.jar to classpath". The red marks in the right margin should now disappear.

  3. Observer that our test methods have no code in them.

  4. Add the following line to each of the method bodies in AuthorTest except for the method setUp

    • TestCase.fail("Not yet implemented");

    The addition of TestCase.fail will cause an issue, use the assistant to resolve the issue. Your file should now look like this

AuthorTest.java
public class AuthorTest {
    @org.junit.Before
    public void setUp() throws Exception {
    }

    @org.junit.Test
    public void getName() throws Exception {
        TestCase.fail("Not yet implemented");
    }

    @org.junit.Test
    public void getEmail() throws Exception {
        TestCase.fail("Not yet implemented");
    }

    @org.junit.Test
    public void getAddress() throws Exception {
        TestCase.fail("Not yet implemented");
    }

}
  1. Our tests now will fail because we explicitly added TestCase.fail("Not yet implemented"); to each test method. To run our tests and see the failures

    • press the green "play" button at the top of the IntelliJ window

    • or press the green "play" button on the left margin on the line that contains the Java class definition header, e.g., public class AuthorTest {

  2. IntelliJ will run your tests and the results of the test run are displayed in a new tab that opens at the bottom of the IntelliJ window.

2.4. Adding a test for getName()

Let’s add a test for the method getName().

  1. We first create an example of an author. Since we are probably going to use this example in more than one test we are going to create

  2. a field in the class AuthorTest to store the example

  3. create an instance of author and set it to our field inside our setUp() method

  4. We then use our example inside a test method in order to call methods defined in the Author class on our instance of Author and verify using JUNit’s Assert.assertEquals that we get the expected values back. Here is the resulting code

AuthorTest.java
import org.junit.Assert;

public class AuthorTest {

    private Author jane;(1)

    @org.junit.Before
    public void setUp() throws Exception {
    
        this.jane = new Author("Jane Doe", "j@a.com", "222 Main St, Seattle, WA, 98980");(2)
    }

    @org.junit.Test
    public void getName() throws Exception {
        
        Assert.assertEquals(this.jane.getName(), "Jane Doe");(3)
    }

    @org.junit.Test
    public void getEmail() throws Exception {
        TestCase.fail("Not yet implemented");
    }

    @org.junit.Test
    public void getAddress() throws Exception {
        TestCase.fail("Not yet implemented");
    }

}
1 We create a new field to store our example
2 We create an instance and set it to our field
3 We use our properly set field to call methods on our instance of Author and validate the method’s response using Assert.assertEquals
For now you can only use Assert.assertEquals on values whose type is a Java reference type not defined by you, i.e., String, Integer, Boolean etc.
Practise Exercises
  • 1) Complete the remaining tests in AuthorTest

  • 2) Draw the UML class diagram for Author

3. Creating new classes

We decided to update our Author class. Instead of holding an author’s name as a string, we would like to create a new class called Person that will hold * a person’s first name, and, * a person’s last name.

Practise Exercises
  • 3) Create the class Person.

  • 4) Create the test class PersonTest and add tests for your class Person.

  • 5) Update your Author class to use Person instead of String for an author’s name.

    • Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  • 6) Draw a new UML class diagram to show how your design has changed not that we added Person.

4. Creating more classes

We also decided that we need a better way to capture email addresses. Using a String is not convenient.

Practise Exercises
  • 7) Design a new class named Email. The class should hold the login name of the email address and the domain. For example the email address joe@a.b.com has

    • the login name john

    • the domain a.b.com

  • 8) Update your Author class to use Email instead of a string for an author’s email.

    • Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  • 9) Draw a new UML class diagram to show how your design has changed not that we added Email.

5. Javadoc

For all the code that you have written up to this point in the lab should have also written documentation for it.

If you have not, go back and add documentation now!

Lets generate the HTML version of your code’s documentation.

  1. Select your project in the Package Explorer tab.

  2. On IntelliJ’s main menu select Tools → Generate Javadoc. A new pop-up window will appear

  3. In the new pop-up window

    1. Select "Whole Project"

    2. Unselect "Include test sources"

    3. Find the line that starts with the text "Output Directory". At the end of that line click the …​ and specify the output folder that you would like IntelliJ to place all the HTML files that will be generated.

    4. Click OK. IntelliJ will run Javadoc and show its progress along with any warnings or errors in the bottom tab of the main IntelliJ window. If the Javadoc generation had no errors then IntelliJ will open your browser and point it to the newly generated documentation.

6. Classes again!

We would like to capture addresses more precisely than just using one long String.

Practise Exercises
  • 10) Design a new class named Address. The class should hold an author’s address. An address is made up of

    • Street number

    • Street name

    • City name

    • State acronym

    • A zip code represented as a number

    • Country name

  • 11) Create a test class named AddressTest and add tests for all methods in the class Address that you wrote/generated code.

  • 12) Update your Author class to use Address instead of a string for an author’s address.

  • 13) Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  • 14) Draw a new UML class diagram to show how your design has changed now that we added Email.

7. Resources