0

Hi I am new to JUnit testing.

I run my JUnit program having selenium code it doesn't run from top to down, it runs randomly.

However i want to execute the program in order, functions like login, creation, updation, Delete.

But, it's running like this

I want to run this program in order. Send me your valuable suggestions.

Nathan Merrill
  • 6,018
  • 5
  • 32
  • 49
user2251940
  • 61
  • 1
  • 3
  • 10
  • 3
    Please take a look at this post: http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4 – Nathan Merrill Aug 09 '13 at 12:13

4 Answers4

0

You can set the classes order of JUnit using test suite:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class
})
public class JunitTestSuite {   
}  

And set the tests order within the class using @FixMethodOrder (since JUnit 4.11)

import org.junit.runners.MethodSorters;

import org.junit.FixMethodOrder;
import org.junit.Test;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {

    @Test
    public void firstTest() {
        System.out.println("first");
    }

    @Test
    public void secondTest() {
        System.out.println("second");
    }
}
Ittiel
  • 1,005
  • 9
  • 12
  • Thanks for responding but i tried with both the answers there is no useful. – user2251940 Aug 12 '13 at 04:59
  • Thanks for responding but i tried with both the answers there is no useful. The order was still problem.I added my sample executed code here please find and provide your valuable solutions. Test suite runner ![enter image description here][1] and normal junit run ![enter image description here][2] – user2251940 Aug 12 '13 at 05:05
0

Annotation List

public interface AnnotationList{


@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface Order {


int value();

}


@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface MyTest {


static class None extends Throwable {

    private static final long serialVersionUID = 1L;


    private None() {


   }}

JUNITLink class

public class JunitLink extends BlockJUnit4ClassRunner {



public JunitLink(Class<?> klass) throws InitializationError {

    super(klass);
}
@Override

protected List<FrameworkMethod> computeTestMethods() {

List<FrameworkMethod> classMethods = getTestClass().getAnnotatedMethods(AnnotationList.MyTest.class);

SortedMap<Integer, FrameworkMethod> sortedTestMethodList = new TreeMap<Integer, FrameworkMethod>();

    for (FrameworkMethod seleniumTest : classMethods) {

        if (seleniumTest.getAnnotation(AnnotationList.Order.class) != null)          {   

        sortedTestMethodList.put(seleniumTest.getAnnotation(AnnotationList.Order.class).value(),seleniumTest);

        }

    }

    return new ArrayList<FrameworkMethod>(sortedTestMethodList.values());

}


@Override

protected void runChild(FrameworkMethod method, RunNotifier notifier) {

    EachTestNotifier eachNotifier = makeNotifier(method, notifier);

if (method.getAnnotation(Ignore.class) != null) {

        runIgnored(eachNotifier);

    } else {

        runNotIgnored(method, eachNotifier);

}


}


private int runNotIgnored(FrameworkMethod method,EachTestNotifier eachNotifier) {

    int failures = 0;

    eachNotifier.fireTestStarted();

try {

        methodBlock(method).evaluate();

} 
catch (AssumptionViolatedException e) {

        eachNotifier.addFailedAssumption(e);



        failures++;

} 
    catch (Throwable e) {

        eachNotifier.addFailure(e);


        failures++;

} finally {

        eachNotifier.fireTestFinished();

    }

    return failures;

}


    private void runIgnored(EachTestNotifier eachNotifier) {


    eachNotifier.fireTestIgnored();

}


    private EachTestNotifier makeNotifier(FrameworkMethod method,RunNotifier notifier) {

    Description description = describeChild(method);


return new EachTestNotifier(notifier, description);

}

}

StartUp Tests

@RunWith(JunitLink.class)

public class StartUp extends SeleneseTestBase {

  public static WebDriver driver;



@Override

@Before
public void setUp()
{

}


@Override

@After
public void tearDown() {

}

TestCases This should extend StartUp class created above

public class Testcase extends StartUp{



public SmokeTest() throws Exception {

}

@Test
@Order(1)
// Wrire test method
}

@Test
@Order(2)
// Test Case 2

}

@Test
@Order(3)
//Test case 3
}
Susanta Adhikary
  • 197
  • 1
  • 5
  • 16
0

You can change the TestName appended with "Test". For Example :

@Test
public void loginTest(){
// Wrire test method
}

@Test
public void creationTest(){
// Test Case 2

}

@Test
public vid updateTest(){
//Test case 3
}

If you have loginTest(), creationTest() and updateTest() instead of login(), create() and update(), then Junit will execute all the test in the order specified.

0

You can use also @FixMethodOrder (MethodSorters.JVM).

For in this case it will be executed in the order in which the methods were implemented.