Lab 04
Short Assignment 04
Introduction
In this lab we will be writing JUnit tests.
Writing software is hard. Plain and simple, programming is not easy. Over the past 70 years there have been many methods developed to make the process of writing code easier. Once such technique is called Test-Driven Development. The idea is simple: you know what you want your code to do, but you do not know how to write code to do it. So start by writing the test cases, then write code that makes those test cases pass. Test-Driven Development is used in industry, and you will most probably do it in CS335. In Java, JUnit is a framework that allows us to write test cases with ease and automate the testing process. Our goal is to introduce you to JUnit to help you test the code you write better.
Set up
Download the BasicArithmetic.java
file from GitHub and set up your working environment on Eclipse with this file.
There are several ways to download the GitHub repo. Click on the green code button:
You can click on Download Zip
or you can copy the HTTPS
or SSH
addresses and then use your shell (terminal, command line) to git clone
it to a specific folder (maybe your working environment folder for Eclipse):
git clone https://github.com/picoral/CSC210-lab04.git
or
git clone git@github.com:picoral/CSC210-lab04.git
Once you have your project set up with the BasicArithmetic.java
file you downloaded from GitHub, you can create a JUnit test file following these steps:
- Go to
New > JUnit Test Case
- Name your file
BasicArithmeticTest
- Click
ok
on the prompt window about adding JUnit library
To use JUnit, each test case (a single method defined like the one below) should be responsible for testing one piece of functionality.
@Test
public void testOneThing() {
}
Writing test cases
Write at least one test case for each of these:
- sum of two numbers
- subtraction of two numbers
- multiplication of two numbers
- division of two numbers
Your JUnit tab should look something like this:
Submission
Submit your BasicArithmeticTest.java
file to Gradescope. Your tests will be manually graded.