Reader small image

You're reading from  jOOQ Masterclass

Product typeBook
Published inAug 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800566897
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

Chapter 19: Logging and Testing

In this chapter, we will cover logging and testing from the jOOQ perspective. Relying on the fact that these are common-sense notions, I won't explain what logging and testing are, nor will I highlight their obvious importance. That being said, let's jump directly into the agenda of this chapter:

  • jOOQ logging
  • jOOQ testing

Let's get started!

Technical requirements

The code for this chapter can be found on GitHub at https://github.com/PacktPublishing/jOOQ-Masterclass/tree/master/Chapter19.

jOOQ logging

By default, you'll see the jOOQ logs at the DEBUG level during code generation and during queries/routine execution. For instance, during a regular SELECT execution, jOOQ logs the query SQL string (with and without the bind values), the first 5 records from the fetched result set as a nice formatted table, and the size of the result is set as shown in the following figure:

Figure 19.1 – A default jOOQ log for a SELECT execution

This figure reveals a few important aspects of jOOQ logging. First of all, the jOOQ logger is named org.jooq.tools.LoggerListener and represents an implementation of the ExecuteListener SPI presented in Chapter 18, jOOQ SPI (Providers and Listeners). Under the hood, LoggerListener uses an internal abstraction (org.jooq.tools.JooqLogger) that attempts to interact with any of the famous loggers, sl4j, log4j, or the Java Logging API (java.util.logging). So, if your application uses any of these loggers, jOOQ...

jOOQ testing

Accomplishing jOOQ testing can be done in several ways, but we can immediately highlight that the less appealing option relies on mocking the jOOQ API, while the best option relies on writing integration tests against the production database (or at least against an in-memory database). Let's start with the option that fits well only in simple cases, mocking the jOOQ API.

Mocking the jOOQ API

While mocking the JDBC API can be really difficult, jOOQ solves this chore and exposes a simple mock API via org.jooq.tools.jdbc. The climax of this API is represented by the MockConnection (for mocking a database connection) and MockDataProvider (for mocking query executions). Assuming that jUnit 5 is used, we can mock a connection like this:

public class ClassicmodelsTest {
 public static DSLContext ctx;
 @BeforeAll
 public static void setup() {
  // Initialise your data provider
  MockDataProvider provider = new ClassicmodelsMockProvider();
 ...

Summary

As you just saw, jOOQ has solid support for logging and testing, proving yet again that it is a mature technology ready to meet the most demanding expectations of a production environment. With a high rate of productivity and a small learning curve, jOOQ is the first choice that I use and recommend for projects. I strongly encourage you to do the same!

Why subscribe?

  • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
  • Improve your learning with Skill Plans built especially for you
  • Get a free eBook or video every month
  • Fully searchable for easy access to vital information
  • Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
jOOQ Masterclass
Published in: Aug 2022Publisher: PacktISBN-13: 9781800566897
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard