博客

Make unit tests readable with Sensei and AssertJ

December 1, 2021
Sean Flanigan

Developers create and test their own code with various methods. One such method is unit testing - a very popular method of testing the smallest parts of one’s own code. When written well, unit tests can help to improve the quality of your code, as well as reduce costs and development time. Whether done as part of test-driven development (TDD) or not, unit testing is a key part of modern development practices and its importance has led to the creation of many testing frameworks.

But in the absence of a proper testing framework and guidance, unit tests can become complex and unreadable as the focus shifts to coverage and quantity rather than quality and readability. This is especially true in a large and active codebase with many unit tests.

A good test framework helps create good readable tests that are simple and focus on quality testing. They also help document the way to use your code as well. One such framework is AssertJ.

AssertJ is a fluent API for writing unit tests in Java. It lets you write test assertions that read like English, with context-aware autocomplete. If you have a large number of tests and need to improve their readability, you may wish to migrate all of them to AssertJ and also ensure that all future tests are taking full advantage of AssertJ.

But complete migration can take an enormous effort and time to do manually and so it is often skipped or put off for other development tasks. So we created Sensei - a highly customizable IntelliJ plugin that can help migrate your current unit tests to use AssertJ as well as enable all future tests to be written the right way. This is done with a number of recipes (rules) already created for you by Sensei’s development team. Sensei makes it easy to perform a one-time migration or take the approach of doing incremental migrations to AssertJ, one test at a time. Once you have moved to AssertJ, the recipes continue to help developers apply quick fixes as they write unit tests so that your unit tests continue to be uniform, standard and consistent across the entire codebase.

‍

Video of how to set AssertJ has size rule

About Sensei

Sensei is a highly customizable IntelliJ plugin to scan and fix undesirable code as you type - with hundreds of downloadable code transformations and migration recipes (rules) as well as an in-built ability to craft your own. With Sensei, developers can remediate bad code patterns as they type so that they can deliver quality code faster and, ultimately write it in a consistent and standard way across teams and projects.

Here is an example of a sample migration that might help you see how Sensei makes it painless to migrate your unit tests to use AssertJ.

Why is AssertJ a great framework to write unit tests

Instead of writing a test like this JUnit assertion:

you can write this with AssertJ:

This means you can read the assertion left to right like an English sentence, and it’s clear which is the expectation (in this case, the size 3). How many times have you written an assertion the wrong way around, like this?

These are just simple examples. The more complex your checks and assertions, the more AssertJ helps you make your tests simple and readable. It works nicely with your IDE’s auto-complete. As soon as you type:

it will pop up with all the things you can check, based on the type of myResult. If it is a collection, you can check its contents, size, whether it contains certain values or types, and so on.

Out of the box, AssertJ has support for primitives and their boxed types, Atomic types, Collections, arrays, Maps, Dates, java.time, Futures, Files/Paths, InputStreams, Throwables, and URLs. And if you have a favorite custom Matcher for Hamcrest, you can use it with AssertJ via HamcrestCondition.

Readable unit tests make it easy for developers to read code in normal English and therefore make it easy to spot mistakes as well as increase coverage - which makes AssertJ an indispensable framework for good Java unit tests.

Recipes to move legacy unit tests to AssertJ

We have written a cookbook to help you write consistent code with AssertJ. It can help you migrate your assertions to AssertJ from any of these frameworks: JUnit 3, JUnit 4, JUnit 5 and FEST-Assert.

This cookbook can be configured in Sensei for immediate use and you can find installation instructions for Sensei here.

Write better unit tests as you code - improving JUnit quality progressively

The Sensei cookbook for AssertJ doesn’t just help you to migrate from other frameworks. It has some recipes which can help you to improve your test writing, break old habits, and write more idiomatic AssertJ tests.

For instance, let’s say you have written a test which looks like this:

Perhaps you even migrated it using one of the migration recipes above, from this JUnit-style assertion:

The Sensei cookbook for AssertJ has a recipe that can also detect this old style of assertion, and offer to convert it to this:

Another recipe can convert the following from:

to this:

A one-time migration and uniform coding practices across teams - with Sensei

We can see from our examples above, the manual migration of unit tests to use AssertJ can involve a lot of effort and time that is best spent on writing better tests. By using Sensei, you can easily migrate your unit tests to AssertJ with confidence. You can perform a one-time migration, or migrate individual tests to AssertJ as you come across them. Sensei provides this type of flexibility that makes code migrations less painful.

Unit test migration is just one example of the many ways Sensei can be used to write consistent code across your projects. You can always be on the lookout for anti-patterns or certain manual code transformations that you frequently come across in pull requests or as you code yourself. If you have a set of coding guidelines that are often missed by developers then you could convert the guidelines into recipes - enabling developers to apply approved code transformations with confidence.

If you have any questions, we’d love to hear from you! Join us on Slack at: sensei-scw.slack.com

标语

在 AI 驱动的开发成果发布前实现有效治理

评估 AI 辅助风险,在提交代码时强制执行安全编码策略,并加速整个软件开发生命周期的安全交付。

预约演示
资源库

探索更多博客

获取有关安全编码、人工智能治理和软件风险管理的专家内容。

浏览全部
案例研究
筛选标签
这是 div 块中的一段文本。

Supercharged Security Awareness: How Tournaments are Inspiring Developers at Erste Group

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

了解更多
案例研究
筛选标签
这是 div 块中的一段文本。

Security as culture: How Blue Prism cultivates world-class secure developers

Learn how Blue Prism, the global leader in intelligent automation for the enterprise, used Secure Code Warrior's agile learning platform to create a security-first culture with their developers, achieve their business goals, and ship secure code at speed

了解更多
案例研究
筛选标签
这是 div 块中的一段文本。

统一的安全文化:Sage 如何打造其安全倡导者项目

了解 Sage 如何通过灵活且以关系为导向的方法提升安全性,成功培养了 200 多名安全倡导者,并实现了显著的风险降低。

了解更多
博客
筛选标签
这是 div 块中的一段文本。

2026年网络安全意识月:理解人工智能安全对企业防御至关重要

目前,大多数顶级人工智能公司已公开呼吁全球放缓人工智能的发展速度,以便安全防护措施能够跟上步伐。然而事实是,严重的安全问题已经显现。随着人工智能实施带来的经济成本和风险不断累积,无数安全领导者和高管团队正处于紧迫的境地。

了解更多
博客
筛选标签
这是 div 块中的一段文本。

赋能要素 8:打造项目品牌

成功的安全编码项目不仅需要优质的内容。赋能要素 8 展示了如何通过品牌化将参与过程转变为一种令人向往、以地位为驱动的体验。

了解更多
博客
筛选标签
这是 div 块中的一段文本。

每一位员工现在都处于 AI 网络安全的最前线

企业技术格局正以一种鲜有人预料到的速度发生剧变,直到为时已晚。我们已正式跨越了从人工编写代码和基础辅助编程,进入“智能体开发生命周期”(ADLC)的时代。虽然自主 AI 智能体有望在多个职能领域实现前所未有的效率,但它们也带来了全新的安全和监管风险。

了解更多

在产品发布前确保 AI 驱动的开发安全

洞察开发风险,执行安全策略,并在整个软件开发生命周期中预防漏洞。

预约演示
未找到相关项目。