Steps to creating and testing methods:

 

Setup for this exercise:

•         Create one new project to hold all of these.  

•         In that project, create an empty class called WorkIt to hold all of your methods.

•         Create another TestIt class to hold the tests of each of the methods.

(Every problem will end up having at least one method in the WorkIt class and another method in the TestIt class.)

 

Step 1: Write the comment for your method

•         Open the class

•         Locate the curly brace that closed the last method. If it is the first, it should go right after the open curly brace for the class.

•         Write the comments:

Step 2 – write the method header

•         Under the bracket, or after the end of another method, write “public static”

•         Write the type of your result (ex: String). If it returns nothing, write “void”.

•         Write the name of your method

•         Between parentheses, write the type of your input followed by the name of the variable your method will use. If you have more than one, separate by commas. Every variable needs a type before it.

•         Type an open and close curly brace.

Step 3 – Send back a dummy result so it will compile

•         Type return

•         Type a value that would be valid for the return type.

•         Type a semicolon

•         Compile to see no errors.

Step 4 – write the test case

•         Open the TestIt class.

•         Locate the curly brace that closed the last method. If it is the first, you should start right after the open curly brace for the class.

•         Type: public static boolean test + the method name + an open and close parentheses and an open and close curly brace:

Ex: public static boolean testProfitOnOneSale()

      {

      }

Step 5 – write one single test case

After the method’s open curly brace {, write the word return and then open and close parentheses () and a semicolon;.

            ex: return ();

Inside the parentheses, write the class.method you are testing (in this case, WorkIt.profitOnOneSale) followed by another () filled with the values you want to test in the order the method expects them.

        ex: return ( WorkIt.profitOnOneSale(5,3));

If the result is a primitive type (not String or another class), type == and the expected value, otherwise type .equals() with the expected value inside.

     ex: return(WorkIt.profitOnOneSale(5,3)== 2);

     ex: return  (WorkIt.mergeCharss’,’a’,’d’).equals(“sad”));

Compile.

Step 6 – write more cases

Put another open parenthesis after the return and close it after the last close parenthesis:

return(WorkIt.profitOnOneSale(5,3)== 2);

-ΰ becomes:

return(

        (WorkIt.profitOnOneSale(5,3)== 2)

                               );

Copy the existing test and put && before it.

return(

        (WorkIt.profitOnOneSale(5,3)== 2)

                               );

-ΰ becomes:

return(

        (WorkIt.profitOnOneSale(5,3)== 2)          &&  (WorkIt.profitOnOneSale(5,3)== 2)

                               );

•         Change the second test case to another good case.

•         Add more as needed. Be sure to test all the borders, and a negative and positive result and input. Don’t forget 0.

Step 7 – adjust for double

•         Doubles don’t hold the exact value, so change your equals statement to a range:

 

 return(WorkIt.profitOnOneSale(5,3)== 2);

-ΰ Becomes:

 return (

                 (WorkIt.profitOnOneSale(5,3) < 2.01)

          &&  (WorkIt.profitOnOneSale(5,3) > 1.99) );

Step 8 – code inside the method

•         Go back to WorkIt

•         Knowing you have a box in memory for every input variable already, code inside the method’s curly braces so that the proper return value will be sent back.

•         This is very specific to every type of problem.

•         Compile

Step 9 – test it

•         Compile and run your unit test.

•         If it fails, use “//” to comment out all but one test. If the result isn’t right, look to see whether the test or method is at fault. If you want to see what the method is producing, set a break point on the method.

•         Keep adding back test lines until they all work.