3

Suppose that I have the below class, a simple class just to add three Strings into a String ArrayList named ar.

public class Testcases {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

}

public ArrayList<String> myArray()    {
    ArrayList<String> ar = new ArrayList<String>();
    ar.add("Customer1");
    ar.add("Customer2");
    ar.add("Customer3");
    return(ar);
 }
}

How could I use Junit testing to make sure that the Strings actually went into ArrayList?

Update

My TestcasesTest file - where the testing is done, looks as follows:

@Test
public void testMain() {
    System.out.println("main");
    String[] args = null;
    Testcases.main(args);
    // TODO review the generated test code and remove the default call to fail.
    // fail("the test case is a resul in the prototype");
}

/**
 * Test of add method, of class Testcases.
 */
@Test
public void testMyArray() {
    assertEquals(Arrays.asList("Customer1", "Customer2", "Customer3"), myArray());
}

}
2
  • Create an array containing the values to test, then go through each value and check that the returned ArrayList has the same length and values of this array. Commented Dec 3, 2014 at 20:49
  • Hey! I added some code in the OP. I created the new 1D array and populated it. I'm just unsure how to compare the ArrayList and Array. Commented Dec 3, 2014 at 21:04

3 Answers 3

3

Following code should do :

@Test
public void myArrayTest()    {
    TestCases testCases = new TestCases();
    List<String> result = testCases.myArray();
    Assert.assertNotNull("List shouldn't be null", result);
    Assert.assertEquals("wrong size", 3, result.size());
    Assert.assertEquals("Wrong 1st element", "Customer1", result.get(0));
    Assert.assertEquals("Wrong 2nd element", "Customer2", result.get(1));
    Assert.assertEquals("Wrong 3rd element", "Customer3", result.get(2));
}
Sign up to request clarification or add additional context in comments.

3 Comments

I like janos' answer better ;)
I get a cannot find symbol class TestCases, location TestCasesTes". I wonder why this would be. The class with my main method is Testcases (lower case c) but even then, I get the same error.
Where did you write your test? Maybe you just need to import the class. Sorry for wrong typing Testcases, but CamelCase says it should be TestCases :)
3

Here you go:

public class Testcases {
    public List<String> myArray() {
        List<String> ar = new ArrayList<>();
        ar.add("Customer1");
        ar.add("Customer2");
        ar.add("Customer3");
        return ar;
    }
}

class TestcasesTest {
    @Test
    public void testMyArray() {
        Testcases testcases = new Testcases();
        assertEquals(Arrays.asList("Customer1", "Customer2", "Customer3"), testcases.myArray());
    }
}

I made some improvements on your method:

  • Use interface types in return type and variable declarations whenever possible. So I changed ArrayList to List in the return type and the local variable
  • No need for the parens in return(ar), this is simpler and natural: return ar

3 Comments

Absolutely not. But his class is called "Testcases", so it looks like this is in fact his test class. The organization of test cases is suspicious here, there isn't enough information to comment about it, so I conveniently side-stepped that potential issue.
I get an error, cannot find symbol myArray() in my TestcasesTest file, this is where the testing is done. Testcase, the main method - is fine though. I've added TestcasesTest to the OP.
I updated my post accordingly, using the same main and test class names as you did.
0

Consider using Assert4J:

  import static org.assertj.core.api.Assertions.assertThat;
  @Test
  public void test() {
    final List<String> result = classUnderTest.someMethod();
    assertThat(result).containsExactly("Customer1", "Customer2", "Customer3");
  }

You'll get very descriptive error failure messages this way:

java.lang.AssertionError: 
Expecting:
 <[5544, 8811, 9988]>
to contain exactly (and in same order):
 <["Customer1", "Customer2", "Customer3"]>
but some elements were not found:
 <["Customer1", "Customer2", "Customer3"]>
and others were not expected:
 <[9988, 8811, 5544]>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.