Submitting Code
1 Overview
Most of the code you write for this class will be submitted electronically using the Computer Science Department's submit system at submit.cs.usna.edu (available on the intranet only).
In case you are not already familiar with how to use this system, check
out the instructions below on using club
or submit
.
Important: You must test your own code thoroughly prior to submission! For most assignments, you will only be able to see a small portion of the auto-testing which will be run after the deadline to help determine your grade. Learning how to think about edge cases and write effective test cases is an important goal for this class.
2 Submitting your code
There are three ways you can submit code for this class.2.1 club
club
is a tool to submit your code
and see the results of the auto-tests right from the comfort
of your own command line.
Read the instructions on the gitlab page here on how to download and install it.
To use club
, you just run
club TheFiles.java ThatYou.java WantTo.txt TurnIn.cpp
When you first run club
, it may ask you to log into the
submit system to copy down your API key. This will then be remembered in
your home directory on that computer.
Running club
for the first time in a folder will also
ask you to select the course and assignment name.
2.2 submit
The submit
program is a more barebones command-line utility
that sends your code to the submit server for any assignment.
Here are the steps to download your personalized submit script:
- Log on to http://submit.cs.usna.edu, using your USNA credentials, and return to this lab page after logging in
-
Make a directory called
bin
in your home directory. This can be done as:mkdir ~/bin
- Click on "Download Personalized Submission Script", saving the file to the bin directory under your home directory
- Open a terminal give the command:
chmod 700 ~/bin/submit
Now if you open a new terminal, you should be able to submit using a command like
2.3 Web interface
If you don't mind the inefficiency, you can use the website at http://submit.cs.usna.edu to manually upload all files for assignment submissions, then wait for the autotesting to complete and check the results.
Always double-check that you are submitting to the correct assignment name and that you submit all files for that assignment.
3 Testing your code
There are primarily two ways to test your Java code for this class. How you do the testing is totally up to you, but when we run your code we will usually be using JUnit.
3.1 Testing using main()
The simplest way to test your code is to make a main()
method in some class, add commands to main
that does whatever
tests you want and prints the results to the screen, and then compile and run
this program to check that your code is working.
This approach can work for simpler/smaller things, but it also has a few drawbacks:
- You have to check the output carefully to make sure everything is correct.
- It is hard to test every edge case and possibility without making the
main
method very long. - For a large assignment, you may test something at the beginning, then forget to go back and test later to make sure you didn't accidentally break it.
3.2 Testing using JUnit4
JUnit4 is a standard and popular Java tool to
automate repeated testing of your code in small, bite-sized pieces. It solves
some of the drawbacks with main()
method testing and is
particularly useful for testing and debugging data structures.
Getting Junit4
First you have to download JUnit4. It should be installed on the lab machines already; on any other Ubuntu installation such as your VM or WSL, just run
sudo apt install junit4
Writing tests
Next, you need to write a class with test cases. Each test case is
just a method in this class that has the @Test
annotation.
Within the test cases, you usually don't print things to the output. Instead,
use the built-in
assertions to check that things are as you expected.
For example, here is a complete JUnit test file with two test cases,
which is checking the functionality of java.lang.String
:
import org.junit.Test;
import static org.junit.Assert.*;
public class StringTest {
@Test
public void lengths() {
String s1 = "something";
assertEquals(9, s1.length());
String s2 = "nothing";
assertEquals(7, s2.length());
assertFalse(s2.isEmpty());
}
@Test
public void characters() {
String s1 = "JUnit4";
assertEquals('U', s1.charAt(1));
}
}
There are many more useful features, including the ability to test that exceptions occur, and lots of other assertions. Check out the Junit4 wiki to learn more.
Compiling and running tests
To compile your test cases, you need to tell Java where to find the jar file for Junit4. The easiest way to do this is to set the CLASSPATH environment variable in bash:
export CLASSPATH=.:/usr/share/java/junit4.jar
Then, you can compile as usual with javac
. To run your tests, you
can do it like this, assuming your test cases are in a file called StringTest.java
:
java -ea org.junit.runner.JUnitCore StringTest
Personally I find it annoying to type this every time, so I make a bash alias like so:
alias ju=(java -ea org.junit.runner.JUnitCore)
Then you can run tests just by typing (for example)
ju StringTest
For even more convenience, edit your ~/.bashrc
file and add the
export CLASSPATH
and alias ju
commands shown above.
Then whenever you start a new terminal those settings will already be applied.