Junit Testing for Multiple Files Upload Spring REST Controller

Introduction Here I am going to write Junit test case for testing multiple files upload in Spring REST Controller. I have already created how to upload multiple files using Spring REST API and I also had shown how to test it through REST client tool – Postman. In this tutorial I am going to use Junit 5, @WebMvcTest annotation to test the multiple files upload REST API. @WebMvcTest annotation can be used not only to test Spring Controllers but also Spring REST Controllers. I will build the applications using both maven…

Continue

Java Junit 5 Mockito doNothing() Example

In this tutorial I am going to show you how to work with Mockito‘s doNothing() in Java Junit 5 API. Mockito‘s doNothing() or Mockito itself is widely used testing framework. If you are working with Junit 4 then you can read the similar example Mockito’s doNothing() using Junit 4. Mockito‘s doNothing() is used when you want to test void methods because void methods do not return anything so there is no way you can verify using assert. These void methods may be anywhere, for example, in service class, in dao…

Continue

Cucumber Tags Example

Introduction In this tutorial we will see how to work on Cucumber Tags. Tags in Cucumber are great ways to organize your features and scenarios. It is pretty much simple when we have one, two, or maybe five scenarios in a feature file. However, in real life project, for each feature, we may have 20, 30, or may be more number of scenarios in a single feature file. They may represent different purpose (Smoke test/Regression test), different prospectives (Dev/Sit/Uat), different status (Ready for execution/Work in progress), etc. Now the question…

Continue

@WebMvcTest Annotation Example in Spring MVC Applications

Introduction We will see the use of @WebMvcTest annotation in Spring MVC applications. The Spring MVC application may be Spring Boot application as well. The @WebMvcTest annotation can be used not only to test Spring Controllers but also Spring REST Controllers. We will build the applications using both maven and gradle tools. @WebMvcTest annotation is used for unit testing of Spring MVC Applications in situation where the test objective is to just focus on Spring MVC Components. In the snapshot shown below, we want to launch only the ToTestController. All…

Continue

Junit 5 Parameterized Tests Examples

Introduction In this example we will create Junit 5 parameterized tests. In our previous example we have seen how to configure Junit 5 for Java projects. In parameterized tests it is possible to run a test multiple times with different arguments. The @ParameterizedTest annotation is used in the regular @Test methods to indicate parameterized tests. In addition, at least one source must be declared to provide the arguments for each invocation in the test method. We will see different sources of data for invoking the tests multiple times with different…

Continue

How to exclude Junit tests using Gradle or Maven in Java Applications

Introduction Here in this tutorial I will tell you how to exclude Junit test cases from build automation in Java applications. It’s not about skipping test cases but it’s about excluding test cases for a particular package. Situations may arise when you want to exclude some Junit test cases from running in some environments (for example, uat, prod, etc.) while you are building your Java based application using gradle or maven build tool. For example, you don’t want to run your Junit tests related to Mosquitto or Mqtt in Jenkins…

Continue

How to configure Junit 5 using Maven and Gradle in Eclipse

Introduction Here I will show you how to configure Junit 5 using maven and Gradle in Eclipse. To get started with Junit 5, you need at least a single TestEngine implementation, for example, junit-jupiter-engine. We will show here both maven and gradle version dependencies for Junit 5. This TestEngine will pull in all the required dependencies. Among those dependencies is junit-jupiter-api which contains the required classes and interfaces your test sources require to compile. junit-platform-engine is also resolved and added. This is the only step that is required to get…

Continue

Mock Activiti TaskService, TaskQuery, NativeTaskQuery in Junit

Introduction In this tutorial we will see how to mock Activiti TaskService, TaskQuery, NativeTaskQuery in Junit class. Suppose you have Spring service class where you are doing some business flow for your application. So you are using Activiti‘s TaskService to create a list of activiti Tasks using the method createNativeTaskQuery(). You may need to create also TaskQuery from TaskService using createTaskQuery() method. Therefore here, we will see how to mock Activiti TaskService, TaskQuery, NativeTaskQuery in Junit class.

Continue

Mock Drools ObjectFactory, KieSession and StatelessKieSession using Junit

This tutorial example will show you how to mock ObjectFactory<KieSession>, KieSession and KieStatelessSession using Junit. KieSession is the most common way to interact with the engine in Drools. A KieSession allows the application to establish an iterative conversation with the engine, where the state of the session is kept across invocations. The reasoning process may be triggered multiple times for the same set of data. After the application finishes using the session, though, it must call the dispose method in order to free the resources and used memory. StatelessKieSession implements…

Continue

Mock ObjectMapper.readValue() using Junit Mockito

Introduction This example will show you how to mock ObjectMapper.readValue() using Junit Mockito. When we write Junit test cases or classes, we generally do not test on real object that are injected into the class that has to be tested using Junit to avoid any test case failure at some point, instead we want to test the business logic out of the written code. We will mock the ObjectMapper class in the same way we mock our external classes, such as, DAO, Repository or any other classes. Here we will…

Continue