0

In my webdriver script I have the three methods

setup, test and tearDown

following the junit convention.

In the test method I have few asserts like this

@Test
public void testStudentHome() throws Exception {
    String classCode = "I6OWW";
    Utilities.studentSignin(driver, baseUrl);
    assertEquals(true, sth.openNotification());
    assertEquals("My Scores", sth.myScores(true));
}

The sth is the PageObject on which I am performing the tests and that I have created in the setup method.

I am calling all these three methods from a main method like this:

public static void main(String[] args) {
        StudentHomeTest sht = new StudentHomeTest();
        try {
            sht.setup();
            sht.testStudentHome();
            sht.tearDown();
        } catch (Exception ex) {
            Logger.getLogger(StudentHomeTest.class.getName()).log(Level.SEVERE, null, ex);
            sht.tearDown();
        }
    }

Now while running the test if some assertion fails the test method should (this is what I expect) throw an exception and the main method should call the tearDown method. But this does not happen. and the browser window continues to stay there. I am using the netbeans ide for running the test.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83

2 Answers2

2

following the junit convention

If you follow the jUnit convention, then you will know that teardown methods belong in the @After method as this method will always run after your tests.

create a new method with the @After jUnit annotation.

@After
public void tearDown() {
  sht.tearDown();
}

Edit

You know what, I believe that you are running into a classic issue of assertEquals in jUnit.

Stolen from this answer...:

JUnit calls the .equals() method to determine equality in the method assertEquals(Object o1, Object o2).

So, you are definitely safe using assertEquals(string1, string2). (Because Strings are Objects)

--
Instead of using assertEquals on these calls, use assertTrue() instead.

assertTrue(sth.openNotification());
assertTrue("My Scores".equals(sth.myScores(true)));
Community
  • 1
  • 1
ddavison
  • 28,221
  • 15
  • 85
  • 110
1

AssertionError doesn't extend Exception - it's a Throwable.

But in any case, you should have

    try {
        sht.setup();
        sht.testStudentHome();
    } finally {
        sht.tearDown();
    }

No need for a catch block. main can throw Exception.

artbristol
  • 32,010
  • 5
  • 70
  • 103