views:

80

answers:

2

I'm using JUnit.I have a test method to test a method and some test cases. I want to run all tes case in that test method, but I can't do that. When first test case fail, test method don't run second test case

Here is my code

public class ComputeServiceTest extends TestCase {

//test add method
public void testAdd()
{
 ComputeServices instance = new ComputeServices();

 //First test case 
    int x1 = 7;
    int y1 = 5;

    int expResult1 = 13;
    int result1 = instance.add(x1, y1);
    assertEquals("First test case fail",expResult1, result1);


    // Second test case
    int x2 = 9;
    int y2 = 6;

    int expResult2 = 15;
    int result2 = instance.add(x2, y2);
    assertEquals("Second test case fail",expResult2, result2);


}

How can I do that?

+3  A: 

The standard advice here would be to put your second test case into a separate method, then it will run regardless of whether or not the first "test case" succeeds or not.

You can use a setUp method to initialize the ComputeServices instance so you don't need that boilerplate in each test method.

nullptr
+1. The comments in the question indicate that the questioner has the term "Test Case" terribly confused. According to the comments, the example should have several different subclasses of TestCase. One class of TestCase for each "Test Case". That would solve the problem, also.
S.Lott
@S.Lott : I don't confuse the term "test case".In my question "test case" is a test case with inputs and expected output(s) but not a subclass of TestCase class.I think your solution is not good because I have to create many classes extend TestCase class to test a method of a class. @unknow: Thanks for your answer.According to your answer, if I want to test a validate email method with 5 test case I have to have 5 method testEmail1, testEmail2,........Is it good if i have many test cases?
Chan Pye
You can have many test methods (testEmail1, testEmail2, testEmail3) per "TestCase" class.If you want to test class Calculator.java the standard is to create CalculatorTest.java that extends TestCase and create as many test methods as you need to test the funcionality that Calculator.java provides. You don't need a class per test method, 1 java class to test --> 1 test class to write (many test methods in it).
Iker Jimenez
+2  A: 

A test case aborts at the first assertion failure, this is by design.

This is to isolate the test cases: if your second assertion fails, how would you know if instance.add(9, 6) is broken, or if this has been caused by the first invocation of the add() method ?

This is useful when the method under test returns an object, or NULL in case of an error. The first assertion ensures the method returned an object, and then it is possible to invoke methods of that object to verify its state. When NULL is returned, the test case aborts on the first assertion an no NullPointerException will be thrown (the pattern is named guard assertion).

It is possible to have as many test methods in a TestCase.

philippe