0

I just started to use TestNG and have a problem with tests. After populating the ArrayList I try to get the first element and after removing it(queue like), but all tests look like work parallel because each test gets the same element, I mean that the second test starts working before the first test remove the element from ArrayList.

    Lab1Main inst = new Lab1Main();
    ArrayList<String> assets;

    
    /**
     * Method for reading input from the console
     * reads only 2 times
     * @return ArrayList<String>
     */
    private ArrayList<String> input() {
        assets = new ArrayList<String>();
        Scanner scan = new Scanner(System.in);
        System.out.println("type your data and press enter at ther end");
        String line = scan.nextLine();
        assets.add(line);
        int count = 0;
        while (count < 1) {
            line = scan.nextLine();
            assets.add(line);
            count++;
        }
        scan.close();
        System.out.println("Scanner closed" + assets);
        return assets;
    }

    
    /**
     * Test will pass or fail depends on input data
     * if input String length less than 3(str.length<3) test will fail 
     * because mutator(setStr) in class Lab1Main will throws an error and won't initialize the variable 
     */
    @Test
    public void firstTest() {
        try {
            inst.setStr(assets.get(0));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println(e);
        } finally {
            Assert.assertEquals(inst.getStr(), assets.get(0));
            assets.remove(0);
        }
    }

    
    
    @Test
    public void secondtTest() {
        try {
            inst.setStr(assets.get(0));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println(e);
        } finally {
            Assert.assertEquals(inst.getStr(), assets.get(0));
            assets.remove(0);
        }
    }

    @BeforeTest
    public void beforeTest() {
        assets = input();
    }

    @AfterTest
    public void afterTest() {
    }

}

Class for testing

    private String str;


    public Lab1Main() {
        // TODO Auto-generated constructor stub
    }

    public String getStr() {
        return str;
    }

    /**
     * throws error if input string length<3
     * @param str
     * @throws StringIndexOutOfBoundsException
     */
    public void setStr(String str) throws StringIndexOutOfBoundsException {
        if (str.length() < 3) {
            throw new StringIndexOutOfBoundsException(
                    "Input string has to has length > 2 your lenght is " + str.length());
        } else {
            this.str = str;
        }
    }
}

Thank you!

Axel
  • 57
  • 5

2 Answers2

1

I mean that the second test starts working before the first test remove the element from ArrayList

Order of tests execution is not guaranteed in TestNG. But there is a parameter called dependsOnMethods that you can use to order your tests like so:

    @Test
    public void firstTest() {
        //first testcase
    }    
    
    @Test(dependsOnMethods="firstTest")
    public void secondTest() {
        //second testcase
    }    
    
    @Test(dependsOnMethods="secondTest")
    public void thirdTest() {
        //third testcase
    }

See TestNG doc for more details. (Do a search for word dependsOnMethods in the link)

Boss Man
  • 587
  • 2
  • 12
  • Does it mean that tests running parallel? – Axel Oct 29 '20 at 13:16
  • 1
    No. Because they are out of order doesn't mean they are running in parallel. TestNG like Junit uses [Reflections](https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) and the order of test methods returned by Reflection depend on JVM's mood. – Boss Man Oct 29 '20 at 19:41
  • Ouuu! Got it! Thank you! – Axel Oct 29 '20 at 23:17
0

I can't say that it's answered my question but it's the way to solve this problem

    public Object[][] sendData() {
        return new Object[][] { { assets.get(0) }, { assets.get(1) } };
    }

    /**
     * Test will pass or fail depends on input data if input String length less than
     * 3(str.length<3) test will fail because mutator(setStr) in class Lab1Main will
     * throws an error and won't initialize the variable
     */
    @Test(dataProvider="sendData")
    public void firstTest(String data) {
        try {
            inst.setStr(data);
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println(e);
        } finally {
            Assert.assertEquals(inst.getStr(), data);
        }
    }
Axel
  • 57
  • 5