Dominic Charley-Roy
https://github.com/dominiccharleyroy
dominic.charley-roy @ mail.mcgill

JUnit is a unit testing framework for Java.
What does that even mean?!
Unit tests are small tests which are responsible for testing a single rule of your program. JUnit provides classes and methods for writing these tests and runs them automatically.


1. Clone your new repository:
git clone url
2. Open Eclipse
3. File > Import > Existing Projects into Workspace
4. Set directory to the directory containing your cloned repository.
5. Select the project an import!



In order to test our implementation, we need to add the JUnit library to our project.
1. Right-click project > Build Path > Add Libraries...
2. Select JUnit and Next
3. Set JUnit version to 4

If you aren't using Eclipse, you have to take some extra steps to install JUnit in your project.
1. Go to https://github.com/junit-team/junit/wiki/Download-and-Install
2. Download junit.jar
3. Add junit.jar to your project's class path

Let's create a test case for BrokenGradingSystem!
1. Right-click test/, New > JUnit Test Case
2. Set the name to BrokenGradingSystemTest.

Once we've added tests, we'll want to run an entire test case. JUnit follows a special process for this based on method annotations. These must be imported (eg. import org.junit.Test).

BeforeClass/AfterClass methods should be static and are only run once. Before/After methods should be non-static and are run before and after every test (for setting up / cleaning up).
Generally put import static org.junit.Assert.*; to have access to all assert methods without qualifying.
The first test is that getGrades returns null for a student that doesn't exist.
We use @Before to create a new Grading System for each test.

We're now ready to run our test! We do this by either hitting the run button when our test file is open, or by right-clicking on the *Test file and Run As > JUnit Test


Note that assertEquals, as do most other assertions, have two signatures.
1. assertEquals(expected value, obtained value)
2. assertEquals(message, expected value, obtained value)
Some tests should only pass if an exception is thrown! There are two ways of testing this.
1. (Preferred way)
@Test(expected=ExceptionName.class)
2. Wrap the code in a try catch and put a fail() after the call which should throw an exception.




First step is to go to https://travis-ci.org/

2.

3.

4. Enabling Travis for your repo
Travis uses a .travis.yml and build.xml file to describe what Travis should do. I've included these files as well as a lib folder in the original repository for Travis.
If you want to use Travis on your own project, the simplest way is to keep the same src/ and test/ structure and copy over travis.yml, build.xml, and the lib folder.
git add .
git commit -m "Initial tests."
git push -u origin masterTravis will take a while to get set up on your first push, so just be patient.
Once we've kicked off our first build, after waiting a minute or so we should be able to see the status by going to the Travis page and clicking on our repository.


Our test helped us pinpoint a bug in getAverage, so let's take a look at our function.

After fixing our bug, we run the JUnit tests and then commit.

