Lab-1-2 - It’s a Mystery!
Tests? Wha’cha mean by tests?
Objective
The objective of this lab is to introduce you to Test-Driven Development (TDD), a widely-used approach in software development. TDD involves writing tests before writing code, ensuring program correctness and completion. The lab aims to familiarize you with the TDD process and tools by providing pre-written tests for your code.
Test-Driven Development
- We’re going to be test-driven this semester. Say what?
- We’re going to learn something about what it’s like to develop using Test-Driven Development (TDD). This is something that all area employers are using at some level. Sometime before the next class session I would like you to read up about it some. Start with these:
- As you move from student to professional, you will need to be able to better answer these questions.
- How do I know my programs work correctly?
- How do I know when I'm done with my program?
- TDD helps us answer both of those questions.
TDD Summary
- Write a test first, before you write your code.
- Run your tests to confirm that your new test fails.
- Write the minimal code that makes the test pass.
- Run all tests to confirm that all tests are now passing.
- Refactor your code when you see an improvement.
- Go to number 1.
OK, but what’s a test?
A test is just code you write that runs your real code to see if it works. If you have a method that performs a calculation, then the test for that method will call it and see if it returns the correct value. What makes TDD interesting is that you write the test first and then your method.
TDD In this Class
- TDD is an advanced concept that is difficult to master at first.
- You really need to be at the level of a pro before it makes a lot of sense. It’s hard to learn programming and TDD at the same time. TDD assumes that you already know how to program.
- We’re going to have you do some of the work of TDD. The instructors have written the tests. Woo hoo! You have to run them and get your code to pass.
- We will start with a simple lab to see how this works.
Instructions
- If you aren’t there already, open Codespace and go to the terminal at the bottom of your development window.
-
Enter this at the prompt.
$ ant run_tests_lab2
- What we want to see is this:
run_tests_lab2: [junit] Running java112.tests.Lab2MysteryClassTest [junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0.032 sec
- Notice the Failures and Errors?
- But, what you will see first is this:
run_tests_lab2: [junit] Running java112.tests.Lab2MysteryClassTest [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.016 sec [junit] Test java112.tests.Lab2MysteryClassTest FAILED
- We have an Error of 1.
-
Run this on the command line: (this is the letter l, not the number 1)
ls -l
- A file named TEST-java112.tests.Lab2MysteryClassTest.txt has been generated in the projects directory. Switch to your editor and open this file. You may need to refresh the directory listing to see the file. Here’s the contents of the file:
Testsuite: java112.tests.Lab2MysteryClassTest Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.016 sec Testcase: initializationError took 0.002 sec Caused an ERROR Ljava112/labs1/MysteryClassOne; java.lang.NoClassDefFoundError: Ljava112/labs1/MysteryClassOne; at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Class.java:2291) at java.lang.Class.getDeclaredFields(Class.java:1743) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) Caused by: java.lang.ClassNotFoundException: java112.labs1.MysteryClassOne at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
- A file named TEST-java112.tests.Lab2MysteryClassTest.txt has been generated in the projects directory. Switch to your editor and open this file. You may need to refresh the directory listing to see the file. Here’s the contents of the file:
Whoa! What's all that?
- We’ll solve this together.
- Hint: Look for the text "Caused by:" It should be around line 14.
- Once you have all tests passing, then run the lab with this (substitute
with the name of the class you want to run, MysteryClassOne, in this case): $ ./runLabs1.sh <class name>
Screenshots
To receive credit for this lab save screenshot(s) in the projects/screenshots/week2
directory.
You may combine the below into one screenshot and name it lab2.png
.
lab2-tests.png
: The command line output showing all unit tests passinglab2-output.png
:The command line output when running MysteryClassOne
In the next lab, you will push this work to GitHub so that your instructor can view it.
Rubric
All of the following must be satisfied to achieve a "Met" status
- Screenshot should clearly show tests passing.
- Screenshot should clearly show expected output when running the program (happy path).
- Code and screenshots are properly named and saved in the correct directory.
- All lab steps have been accurately and appropriately implemented.
- Code adheres to the course coding standards.
- Commit messages are concise, atomic, and effectively describe the change(s) made.
- JavaDoc documentation is properly implemented, providing clarity and understanding of the code's functionality and usage.
- External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.