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.
-
Go to the section with the title "Java SE Development Kit 8u92"
-
Click on the radio button with the title "Accept License Agreement"
-
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
-
-
Allow the download to complete
-
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.
-
-
As a student you get the full version of the IDE for free. Apply for the student discount first.
-
Download IntelliJ (any verion will do)
-
Install IntelliJ on your machine using the process that you typically use for your operating system to install new software
-
2.3. Test your Setup
-
Clone your git repository to a folder on your computer.
-
Open IntelliJ
-
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
-
-
Select
Java
from the list of project types on the left -
Using the dropdown on the right select
1.8
as your projects JDK version -
Click Next. The contents of the window will change
-
Select nothing in this window and click Next. The contents of the window will change
-
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.
-
-
Click Finish
-
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.
-
-
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. -
You should see a sub-folder named
src
-
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. -
A small window will pop-up asking you to provide a name for your class. Input the name
Author
and click OK -
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
-
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
-
-
Notice that in the Project Explorer tab there is now a new file named
Author
under the folder namedsrc
-
To test that your setup is working as expected, replace all the contents of the file
Author.java
with the following code
-
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 headerpublic 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
-
-
Click on the yellow light bulb icon and a menu should appear.
-
From the menu select Create Test. This action will cause a new pop-up window to appear.
-
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 -
For "Testing library" select
JUnit 4
-
Click the Fix button to add JUnit4 to your project. A new pop-up window will appear.
-
Select the first option with the title "Use JUnit4 from IntelliJ IDEA distribution"
-
Click OK. This will close this pop-up window and takes us back to continue setting up our test for
Author
. -
Back in the pop-up for setting up our test for
Author
leave "Superclass" empty. -
Leave "Destination Package" empty
-
Select the check mark with the title "setUp/@Before" only.
-
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. -
Click OK
-
Select
JUnit Test Case
from the pop-up menu. This will create a new Java class (and a new file) calledAuthorTest
. If IntelliJ asks you to add this new file to your repo click No. -
The Package Explorer should now have a new file with the name
AuthorTest
under the foldersrc
-
The editor should now display
-
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). -
From the assistant’s pop-up menu select "Add junit.jar to classpath". The red marks in the right margin should now disappear.
-
Observer that our test methods have no code in them.
-
Add the following line to each of the method bodies in
AuthorTest
except for the methodsetUp
-
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 -
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");
}
}
-
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 {
-
-
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()
.
-
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
-
a field in the class
AuthorTest
to store the example -
create an instance of author and set it to our field inside our
setUp()
method -
We then use our example inside a test method in order to call methods defined in the
Author
class on our instance ofAuthor
and verify using JUNit’sAssert.assertEquals
that we get the expected values back. Here is the resulting code
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.
|
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.
4. Creating more classes
We also decided that we need a better way to capture email addresses. Using a String
is not convenient.
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.
-
Select your project in the Package Explorer tab.
-
On IntelliJ’s main menu select Tools → Generate Javadoc. A new pop-up window will appear
-
In the new pop-up window
-
Select "Whole Project"
-
Unselect "Include test sources"
-
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.
-
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
.