Home » JUnit

Tag Archives: JUnit

A Comprehensive Guide On JUnit 5 Extensions

JUnit is one of the most popular unit testing frameworks in the Java ecosystem. The JUnit 5 version (also known as Jupiter) contains many exciting innovations, including support for new features in Java 8 and above. However, many developers still prefer to use the JUnit 4 framework since certain features like parallel execution with JUnit 5 are still in the ...

Read More »

TestNG vs. JUnit Testing Framework: Which One Is Better?

Introduction Software Development goes through many phases like requirement gathering & analysis, communication, design, code building, testing, and release. A business has to ensure that the product they deliver is up to the mark, which is possible when the product goes through multidimensional quality checks. Testing is an indispensable part of the SDLC and can be achieved either manually or ...

Read More »

Testing with Mockito

1) Register MockitoExtension @ExtendWith(MockitoExtension.class) class ObjectTest { static final Long ID = 1L; 2) Create the mock @Mock private ObjectRepo mockRepo; 3) Inject the mock @InjectMocks private ObjectService objectService; @Test void whenfindByIdThenReturnResult() { var objectDAO = new ObjectDAO(); objectDAO.setId(ID); 4) Define the behavior when(mockRepo.findById(any(Long.class))).thenReturn(Optional.of(objectDAO)); 5) Test var result = ObjectService.findById(ID); 6) Verify verify(mockRepo, times(1)).findById(any(Long.class)); 7) Validate assertAll( () -> assertNotNull(result), ...

Read More »

Testing Expected Exceptions with JUnit 5

This post shows how to test for expected exceptions using JUnit 5. If you’re still on JUnit 4, please check out my previous post. Let’s start with the following class that we wish to test: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 public class Person {   private final ...

Read More »

Extending JUnit 5

A look into the past With JUnit 4 we have the option to run test with a custom JUnit runner (indicated by the @RunWith annotation). This allows us to modify the way tests are executed with JUnit. However, JUnit runners are not that easy to implement. They also suffer on the major limitation that only one runner can be used ...

Read More »

Microservices in Publish-Subscribe communication using Apache Kafka as a Messaging Systems and validated through Integration Test

Publish-Subscribe Messaging systems play an important role in any enterprise architecture as it enables reliable integration without tightly coupling the applications. The ability to share data between decoupled systems is not a problem that is easily tackled. Consider an enterprise with multiple applications that are being built independently, with different languages and platforms. It needs to share data and processes ...

Read More »

JUnit 5 and Selenium – Using Selenium built-in `PageFactory` to implement Page Object Pattern

Selenium is a set of tools and libraries supporting browser automation and it is mainly used for web applications testing. One of the Selenium’s components is a Selenium WebDriver that provides client library, the JSON wire protocol (protocol to communicate with the browser drivers) and browser drivers. One of the main advantages of Selenium WebDriver is that it supported by ...

Read More »

Spring Boot testing with JUnit 5

JUnit 5 (JUnit Jupiter) is around for quite some time already and it is equipped with tons of features. But surprisingly JUnit 5 it is not a default test library dependency when it comes to the Spring Boot Test Starter: it is still JUnit 4.12, released back in 2014. If you consider using JUnit 5 in you next Spring Boot ...

Read More »

Seven Testing Sins and How To Avoid Them

Throughout this article I will be using Java within code snippets, whilst also using JUnit and Mockito. This article aims to provide examples of test code which can be: hard to readdifficult to maintain Following these examples the article will attempt to offer alternatives, which can be used to enhance the readability of your tests, which in turn will help ...

Read More »