TestNG

In TestNG (http://testng.org/doc/index.html), tests are organized in classes, just as in the case of JUnit.

The following Gradle configuration (build.gradle) is required in order to run TestNG tests:

dependencies { 
   testCompile group: 'org.testng', name: 'testng', version: '6.8.21' 
} 
 
test.useTestNG() { 
// Optionally you can filter which tests are executed using 
// exclude/include filters // excludeGroups 'complex' }

Unlike JUnit, TestNG requires additional Gradle configuration that tells it to use TestNG to run tests.

The following test class is written with TestNG and is a reflection of what we did earlier with JUnit. Repeated imports and other boring parts are omitted with the intention of focusing on the relevant parts:

@BeforeClass 
public static void beforeClass() { 
  // This method will be executed once on initialization time 
} 
 
@BeforeMethod 
public void before() { 
  friendships = new Friendships(); 
  friendships.makeFriends("Joe", "Audrey"); 
  friendships.makeFriends("Joe", "Peter"); 
  friendships.makeFriends("Joe", "Michael"); 
  friendships.makeFriends("Joe", "Britney"); 
  friendships.makeFriends("Joe", "Paul"); 
} 

You probably already noticed the similarities between JUnit and TestNG. Both are using annotations to specify what the purposes of certain methods are. Besides different names (@Beforeclass versus @BeforeMethod), there is no difference between the two. However, unlike Junit, TestNG reuses the same test class instance for all test methods. This means that the test methods are not isolated by default, so more care is needed in the before and after methods.

Asserts are very similar as well:

public void alexDoesNotHaveFriends() { 
  Assert.assertTrue(friendships.getFriendsList("Alex").isEmpty(), 
      "Alex does not have friends"); 
} 

public void joeHas5Friends() { Assert.assertEquals(friendships.getFriendsList("Joe").size(),
5, "Joe has 5 friends"); }
public void joeIsFriendWithEveryone() { List<String> friendsOfJoe =
Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul");
Assert.assertTrue(friendships.getFriendsList("Joe")
.containsAll(friendsOfJoe)); }

The only notable difference when compared with JUnit is the order of the assert variables. While the JUnit assert's order of arguments is optional message, expected values, and actual values, TestNG's order is an actual value, expected value, and optional message. Besides the difference in the order of arguments we're passing to the assert methods, there are almost no differences between JUnit and TestNG.

You might have noticed that @Test is missing. TestNG allows us to set it on the class level and thus convert all public methods into tests.

The @After annotations are also very similar. The only notable difference is the TestNG @AfterMethod annotation that acts in the same way as the JUnit @After annotation.

As you can see, the syntax is pretty similar. Tests are organized in to classes and test verifications are made using assertions. That is not to say that there are no more important differences between those two frameworks; we'll see some of them throughout this book. I invite you to explore JUnit (http://junit.org/) and TestNG (http://testng.org/) by yourself.

The complete source code with the preceding examples can be found at https://bitbucket.org/vfarcic/tdd-java-ch02-example-testng.

The assertions we have written until now have used only the testing frameworks. However, there are some test utilities that can help us make them nicer and more readable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.96.155