Skip to content

Test Writing for Code

Sharina Stubbs edited this page Sep 17, 2019 · 4 revisions

Files to write in

Tests get written in LibraryTest.java

Method gets written in Library.java

How many tests to write? Given limited time, write 2-3. Add in comments along the way of what tests I would write if I had time.

Write enough tests that you are mostly convinced the code will work.

If you had enough time and were building a well tested app, you should write many tests.

Other tests to write, besides those in the below examples.

  • Test an array with 1 string
  • An array with no strings
  • Array that contains duplicate longest strings
  • Longest string in beginning of array
  • Longest string at end of array

Example

Within LibraryTest.java file:

We specify which test to run by putting @Test just above the method

@Test
public void testLongString() {
    Library.LongString(new String[]{"a", "abcd", "ab"});
    assertEquals( 
        message: "in this array, abcd should be the longest string", 
        expected: "abcd", "ab",
        Library.longString(new String[]{"a", "abcd", "ab"})
    );
}
@Test
public void testLongStringLength(){
    assertEquals(
        message: "spaces should count towards length",
        expected: "hello world",
        Library.longString(new String[]{"helloworld", "hello world"})
    );
}

Within Library.java file:


    public static String longString(String[] arr) {
        String answer = arr[0];
        for(int i =1; i < arr.length; i++) {
            answer = arr[i];
        }
        return answer;
    }
}
    

For each class

Create it's own test file.

Clone this wiki locally