In this tutorial we will see how to write Junit test cases on Spring REST services. Before continue to reading this tutorial please read first Spring RESTful Webservice CRUD Example
We have total four operations on this REST example. If you want you may add more operation such as find all products and apply Junit on it.
Continue reading “Junit Testing Spring REST Services”
You can find maven based Junit Code Coverage https://www.jeejava.com/junit-code-coverage/ This tutorial will show you Gradle based Junit Code coverage.
Continue reading “Junit Code Coverage using Gradle, Jacoco”
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. Continue reading “JUnit Code Coverage”
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. Continue reading “Junit’s annotation based expected exception testing”