Log in from a computer to take this course

You'll need to log in from a computer to start Learn Intermediate Java. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

“Red — Green — Refactor!”

By the end of this lesson, this mantra should be playing in your mind whenever you think about testing your code.

This mantra emphasizes the “test-first” approach to programming. In the simplest sense, this just means that a program’s tests are written before the code is made.

But how does that work? How can a test be written to test code that doesn’t exist yet?

The answer lies in the mantra.

“Red” is the start of the mantra and the first thing a programmer needs to do in test-first development. In other words, they need to write a test and make it go red as the first step for testing.

The act of doing this establishes a test to exist first, which is good practice for coverage but also lays the requirements for the code. A skeleton of the method they want to be testing is then created, which often will be set to return 0 (or some other arbitrary value) to ensure the test has something to compare against. This arbitrary value will be different from what the test is “expecting” and make the test go red.

“Green” is the second step of the mantra. Now that we have our red test, we can write code into the skeleton method to make it work. We then run the test again and watch the test method go green.

These two steps help ensure the following things: we have good test coverage, our tests are testing what they should be testing, and the code works as expected.

The last step of the mantra is “Refactor,” which is often the last “clean-up” step of any coding. Basically, this step cleans up and polishes the code now that its bare minimum processing is tested and functional.

Now, let’s explore what makes these @Test methods tick and why this junit.org.Assert class library is so important.

Instructions

1.

TestExample.java is a simple example with an associated java class. It has a @Test method already written out. For now, don’t worry about what the assertEquals() is doing.

The test method is checking to see if the class variable a is a positive number, and will pass if it is. isPositive(int n) will return 1 if n was positive, and 0 if negative.

For the sake of simplifying the exporting process, let’s just use a batch file to execute the 3 commands we’ve been using. Now, to compile and run your code, enter the following into the terminal:

$ ./run.bat

Run the test class and watch the test fail, and make sure to Check Work in order to progress.

2.

In TestExample.java, change a to a value that will make the test method pass.

To compile and run your code, enter the following into the terminal:

$ ./run.bat

Run the test class again and watch it pass this time, then Check Work.

3.

Let’s check a border case, which is something that JUnit testing is most commonly used for.

In TestExample.java, declare a new integer c and set it equal to 0. 0 is notorious for being neither positive nor negative in math, so let’s reflect this in the code.

Add assertThat(ex.isPositive(c), equalTo(-1)); to the list of assert methods inside the test method.

Now add a special case to the .isPositive() method inside Example.java that returns -1 if a 0 was passed in as the input.

To compile and run your code, enter the following into the terminal:

$ ./run.bat

Run the test class and Check Work. All test cases should pass.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?