Simple Test

There's no better way to get going than by looking at real code. This example uses the TestBase class as the main class to extend from.

This isn't a requirement with TestNG - but it does make some things a lot easier. (such as managing the mock objects created with EasyMock.)

import org.testng.annotations.Test;

@Test
public class StringTest extends TestBase {

    public void test_String_Length()
    {
        String str = "simple";
        
        assertEquals(str.length(), 6);
    }

    public void test_String_Equals()
    {
        assert "simple".equals("simple");
    }
}

You'll notice that we used the @Test annotation at the class level instead of each individual method. TestNG is very flexible in how you can tell it to run your tests, in our example we didn't need to configure each method differently so using the single @Test annotation at the top of the class is more than enough to tell TestNG that this is a test class.

Assertions

The previous example used a assertEquals(actual, expected) utiltity method to do equality checking. It is provided in the TestBase class as a convenience, but you could do the same thing in your own tests with a static import of the Assert class like this:

import org.testng.annotations.Test;
import static org.testng.Assert.*;

@Test
public class StringTest {

    public void test_String_Length()
    {
        String str = "simple";

        assertEquals(str.length(), 6);
    }
}