04 Jun

Junit Code Coverage using Gradle, Jacoco

You can find maven based Junit Code Coverage http://www.jeejava.com/junit-code-coverage/ This tutorial will show you Gradle based Junit Code coverage.

Prerequisites

IntelliJ IDEA
Gradle 3.5
Java 1.8
Read More

13 Dec

JUnit Suite Test Example

In this example I am going to explain how to use a test suite in JUnit testing framework. A test suite bundles a few unit test cases and runs them together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.

JUnit Classes : JUnit classes are important classes, used in writing and testing JUnits. Some of the important classes are – Assert, TestCase, TestResult.

Test Runners : Test runner is used for executing test cases. Read More

19 Nov

JUnit Code Coverage

JaCoCo is quite a new maven plug-in that provides the JaCoCo runtime agent to your tests and allows basic report creation. Currently it supports instruction, branch, line, method and class coverage which is pretty enough you can expect from this kind of tool. Additionally, it can measure and report cyclomatic complexity for methods and summarize the complexity for classes and packages.

So if you want to have line number information included in the coverage reports or you want source code highlighting the class files of the test target must be compiled with debug information.

Note when it is used in conjuction with the maven-surefire-plugin or maven-failsafe-plugin you must not use a forkCount of 0 or set the forkMode to never as this would prevent the execution of the tests with the javaagent set and no coverage would be recorded. Read More

10 Mar

Check a given date is first day of month using joda api

In this post, I will show you how to check whether a given date is first day of the month using joda date time API.

Prerequisites

The following configurations are required in order to run the application

Eclipse
JDK 1.8
Have maven installed and configured
Joda date API dependency in pom.xml

Now we will see the below steps how to create a maven based Java project in Eclipse Read More

02 Mar

Junit’s annotation based expected exception testing

Junit provides an option of tracing exception thrown by a Java method. There are different ways of handling exceptions in JUnit tests. Standard Junit’s org.junit.Test annotation offers expected attribute that allows us specifying the a Throwable to cause a test method to succeed if an exception of the specified class is thrown by the method. A Java unit test should verify correct exception thrown in exceptional case and no exception should be thrown in normal case. For this to test exception thrown by a Java method using @Test(expected=””) we need to use at least Junit 4.7. One of the many situations where we need to test exception thrown by a Java method is testing an API method that should throw IllegalArgumentException if arguments passed to the method are not matching to pre-conditions. For example, in order to test that we need to use @Test(expected=IllegalArgumentException.class) annotation. We can replace IllegalArgumentException.class with any other exception e.g. NullPointerException.class or ArithmeticException.class etc as per the requirment. In order to test any Java method for throwing excpetion, we need to ensure that arguments provided to the method, from the test must result in expected Exception, otherwise JUnit test will fail. Read More

25 Feb

Create archive or zip using Java

With this example I will show you how to create an archive or zip from multiple files using Java language.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Read More

15 Feb

Generic way of writing data to multiple sheets in excel using apache poi

With this example I will show you how to create an excel file and write any kind of list of objects to multiple sheets in excel file using Apache POI in Java language.

The Apache POI is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). You can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is the Java Excel solution (for Excel 97-2008).

For more information please go through https://poi.apache.org/

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Read More

14 Feb

Generic way of writing data in excel using apache poi

With this example I will show you how to create an excel file and write any kind of list of objects to excel file using Apache POI in Java language.

The Apache POI is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). You can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is the Java Excel solution (for Excel 97-2008).

For more information please go through https://poi.apache.org/

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Read More

19 Jan

Using Jetty Server with Junit test

In this post I will show you how to use Jetty server with Junit test case and how to deploy web application using Java program in Junit test.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Read More

13 Jan

JUnit Mockito doNothing Example

In this post, I will be showing one of the most widely used and popular JUnit Testing Mocking framework – Mockito. Mockito is one of the widely used testing API for Java.

Prerequisites

The following configurations are required in order to run the application

Eclipse
JDK 1.7
Have maven installed and configured
Junit, Mockito, PowerMockito dependency in pom.xml

Now we will see the below steps how to create a maven based Java project in Eclipse Read More

Share