Home » Tag Archives: JUnit

Tag Archives: JUnit

JUnit 5 – Basics

JUnit 5 is the next generation unit testing framework for Java equipped with many interesting features including nested tests, parameterized tests, new extension API or Java 8 support to mentioned a few. This article shows basic concepts of JUnit 5 including test lifecycle, parameter injection and assertions (basic, timeout and exception). Documentation First of all, JUnit 5 documentation is just ...

Read More »

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 »