Home » Tag Archives: JUnit

Tag Archives: JUnit

Lifecycle of JUnit 5 Extension Model

JUnit5 final release is around the corner (currently it is M4), and I have started playing with it a bit on how to write extensions. In JUnit5, instead of dealing with Runners, Rules, ClassRules and so on, you’ve got a single Extension API to implement your own extensions. JUnit5 provides several interfaces to hook in its lifecycle. For example you can hook to  Test Instance ...

Read More »

Cleaner parameterized tests with JUnit 5

The general idea of parameterized unit tests is to run the same test method for different data. Creating parameterized tests in JUnit 4 is far from being perfect. There are many issues with the existing architecture: parameters are defined as class fields and constructor is needed to create them, parameterized and non-parameterized tests cannot be mixed in one test class ...

Read More »

Testing exceptions with JUnit 5

JUnit 5 brought pretty awesome improvements and it differs a lot from its predecessor. JUnit 5 requires Java 8 at runtime hence Lambda expressions can be used in tests, especially in assertions. One of those assertions is perfectly suited for testing exceptions. Setup the project To demonstrate JUnit 5 usage I used my long-lived unit-testing-demo Github project as it already ...

Read More »

JUnit 5 – Parameterized Tests

JUnit 5 is pretty impressive, particularly when you look under the covers, at the extension model and the architecture. But on the surface, where tests are written, the development is more evolutionary than revolutionary – is there no killer feature over JUnit 4? Fortunately, there is (at least) one: parameterized tests. JUnit 5 has native support for parameterizing test methods ...

Read More »

Importance of given-when-then in unit tests and TDD

Recently, I’ve been writing rather about more advanced concepts related to automatic testing (mostly related to Spock). However, conducting my testing training I clearly see that very often knowledge of particular tools is not the main problem. Even with Spock it is possible to write bloated and hard-to-maintain test, breaking (or not being aware of) good practices related to writing ...

Read More »

Expected Exception Rule and Mocking Static Methods – JUnit

Today I was asked to consume a RESTful service so I started implementing it following Robert Cecil Martin’s rules for TDD and came across a new way (atleast for me) of testing the expected exception along with the error message so thought of sharing the way I implemented it as part of this post. To start with let’s write a ...

Read More »

Data driven testing with Junit & easytest

In this article we are going to see how can we make a Data Driven testing with Junit. For this I am going to use a library called EasyTest. For TestNG , as we know, it has built in data provider. Using easy test, we cam use Junit to do data driven tests. What is Data Driven Testing? When you ...

Read More »

Using Kafka with Junit

One of the neat features that the excellent Spring Kafka project provides, apart from a easier to use abstraction over raw Kafka Producer and Consumer, is a way to use Kafka in tests. It does this by providing an embedded version of Kafka that can be set-up and torn down very easily. All that a project needs to include this support is ...

Read More »

Create JNDI resources for JUnit testing using Spring

Until recently, I had static methods setting up my in memory database (HSQLDB). I called these methods in setUp/tearDown of my JUnit tests. This felt always a bit unnatural to me as I use Spring and everything should run through it’s application context. Creating a simple JNDI bean As I use JNDI in production, I had to created a JNDI ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns