Posts

Showing posts with the label Unit Tests

TestNG Annotations Explained In Detail

In this article, I will be explaining some of the important TestNG annotations. Introduction TestNG is a popular unit testing framework. It allows creating and running tests. Though largely inspired by JUNIT, it has many advanced features like generating HTML reports, running parametrized tests, and so on. One of the key features of TestNG is its ability to be configured via annotations. Annotations can be embedded within the TestNG code and help to control the flow of test execution. TestNG Annotations Let us now take a closer look at the TestNG annotations. @Test Designates a method as a TestNG test. Thus, you need to specify this annotation on any method that you want to run as a TestNG test. The @Test annotation has several attributes. Some of these are as follows: group: Specifies the group that the test belongs to. (TestNG allows creating test groups, so you can group related tests and run them in one go) description: Specifies the test description enabled: Specifies whether tes...

How to use TestNG with Maven and Eclipse

Image
Earlier, we had seen how to use JUNIT for unit testing. TestNG is another such unit testing framework. In this article, we will be learning how to use TestNG with Maven and Eclipse. TestNG Overview TestNG is a very powerful unit testing framework. Not only does it allow creating and running tests, but it has many advanced features like generating test reports, parameterizing tests, and much more. Let us now learn how to use TestNG to test a simple Java program. Project Creation and Setup  Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the TestNG dependencies (You can refer to this blog post). Alternatively, you can add the following to your pom.xml file: <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope></dependency> Writing and Running Code Step 3 – Write code to be tested...

How to use JUnit to unit test code

Image
In this blog post, I will be explaining how you can run JUnit unit tests to test your code. What is JUnit? JUnit is a unit testing framework for Java. It is used by developers to test individual code and ensure that code works the way it was intended to. The current version of JUnit is JUnit 5 .   Why JUnit? Consider the following code snippet: package demo;public class MathDemo {public int add(int a,int b){return a+b;}public int subtract(int a,int b){return a-b;}}   So this class has two methods, called add and subtract . The add method adds 2 numbers and returns the sum while the subtract methods returns the difference of the 2 input numbers.  Now traditionally, if you want to test if this code works, you would write a main method like this: public static void main(String args[]){MathDemo mathDemo = new MathDemo();int sum = mathDemo.add(5,4);int diff = mathDemo.subtract(9, 3);System.out.println("Sum is "+sum);System.out.println("Difference is "+diff);...