JPragma Blog

Pragmatic Developers, Elegant Software

Static content and webjars in Micronaut applications

leave a comment »

Sometimes I need to add a very simple UI to my Micronaut applications. Lightweight javascript libraries such as JQuery or AngularJS are quite convenient for this purpose. Traditionally CDNs are used to reference and download these libraries at runtime, but I prefer to use webjars. This way my app will continue to work even if there is no internet connection or if certain CDN is blocked by e.g. corporate network policies.

Step 1

Add dependency to the webjar. In gradle this would look like:


Step 2

Add following configuration to application.yml

    name: myapp
          - "classpath:public"
          - "classpath:META-INF/resources/webjars"

Step 3

Create static resources (html, js, css) under src/main/resources/public

Step 4

Reference webjar javascript library from html

    <script src="jquery/3.5.1/jquery.min.js"></script>

Written by isaaclevin

February 28, 2021 at 4:39 pm

Posted in Uncategorized

Integration testing of a legacy code

leave a comment »

Wouldn’t it be nice to work only on greenfield projects with the team that shares the same vision and style? Unfortunately, we have to spend a significant portion of our professional life dealing with the messy legacy code. Such code is very hard to comprehend. It often consists of tangled classes, with some arbitrary division of responsibilities. Unit test coverage is often very low. Sometimes unit tests are formally there, but you can clearly see that they have been written after the fact, simply repeating the production code mess. These tests are very fragile and don’t provide adequate “safety net”.

Before trying to refactor such legacy code we must first create good end-to-end tests. Tests that are, as Kent Beck says, “sensitive to changes in system behavior, but insensitive to changes in code structure”.

Let’s assume your project is a typical SpringBoot application. We have some complex functionality, that works, but we don’t fully understand how. We want to do some “forensic analysis” of its implementation and cover it with a stable integration e2e test. We know that this functionality starts with externally exposed REST API. We know that after going through layers of services, managers, helpers, utility classes, repositories, etc. it stores some data in the database and also makes some calls to external systems using REST and/or JMS.

Here is the plan:

  1.  Assuming JUnit 5, create test class “XyzComponentIT” and annotate it with @ExtendWith(SpringExtension.class) and @ContextConfiguration(classes=XyzConfig.class). This XyzConfig class should be annotated with @TestConfiguration and will be used to create Spring beans using @Bean instead of relying on a component scan. This way we will know exactly which beans participate in our workflow.
  2.  Create the first bean for the entry point (REST controller) and inject it into the test using @Autowired
  3.  Create a test case method that invokes this entry point passing some typical payload into it.
  4.  If you try to run the test at this point, it will fail to initialize the Spring context – this is expected, we didn’t provide any controller’s dependencies yet. Let’s start creating them one by one.
  5.  Since we are writing end-to-end integration tests, we want to create beans using a real implementation of every dependency, except “edge components”. Edge components are those that encapsulate communication with external systems – e.g. database repositories, HTTP and JMS clients, etc.
  6. Beans of edge components should be mocked. One very useful technique here is to use “strict mocks”. “Strict mocks” fail on every call that is not explicitly stabbed. This way we can identify the exact external system communication and crosscheck it with the business requirements. Below I included a snippet of  Mockito based implementation of such strict mocks. For better readability, consider encapsulating such mock beans in properly designed test doubles.
  7.  Next, implement the actual test case method, stubbing calls that read external data and verifying essential interactions with the external systems. For example, if our workflow updates the customer profile in the database and sends notifications via email service, then we should verify all interactions with test doubles that encapsulate DAO and email server communication.

Integration tests described here are quite stable since they have minimal knowledge of the internal structure and implementation details of our components. Since they consider the whole system as a black box, they are an essential tool that gives us confidence while doing code refactoring.

Here is the GIST of a very simplified example:

package com.jpragma.jtest;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import java.util.Arrays;
public class MockitoExtensions {
private static Answer<?> alwaysThrowingAnswer = (invocation > {
throw new IllegalArgumentException("Unexpected call to " + invocation.getMethod() +
" with " + Arrays.toString(invocation.getArguments()));
public static <T> T strictMock(Class<T> clazz) {
return Mockito.mock(clazz, alwaysThrowingAnswer);

import com.jpragma.jtest.MockitoExtensions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ContextConfiguration(classes = XyzComponentIT.Config.class)
public class XyzComponentIT {
XyzController controller;
AddressValidator addressValidator;
CustomerRepository customerRepository;
EmailServiceClient emailServiceClient;
void updateCustomerProfile() {
CustomerProfile customerProfile = new CustomerProfile()
new PrimaryAddress()
.setStreet("1 Main st")
String email = "";
doNothing().when(emailServiceClient).sendEmail(anyString(), anyString());
verify(emailServiceClient).sendEmail(email, "Profile Update");
static class Config {
XyzController controller() {
return new XyzController(profileUpdateService());
ProfileUpdateService profileUpdateService() {
return new ProfileUpdateServiceImpl(addressValidator(), customerRepository(), emailServiceClient());
AddressValidator addressValidator() {
return MockitoExtensions.strictMock(AddressValidator.class);
CustomerRepository customerRepository() {
return MockitoExtensions.strictMock(CustomerRepository.class);
EmailServiceClient emailServiceClient() {
return MockitoExtensions.strictMock(EmailServiceClient.class);

view raw
hosted with ❤ by GitHub


Written by isaaclevin

April 10, 2020 at 11:23 am

Posted in Java, Testing

Toughts on code reviews and dev process

with one comment

Nowadays most software projects set the master branch to be read-only, use pull requests to merge feature branches and require code reviews with multiple approvals. Undoubtedly this a good practice in theory, however, as usual, the devil is in the detail, and quite often typical code reviews are useless and even harmful.

  • Most standard code review tools display all changes in a flat list, so the context of these changes is completely lost. The order, in which files appear has nothing to do with the logical organization of the change (and the codebase in general).
  • It is extremely hard to reason about changes larger than just a few files with more than just 10-20 added/modified lines of code in each – especially when these changes mix design concepts and low-level implementations of particular functions. It requires a very deep context switch and extreme discipline from the reviewer.
  • As a result, many reviews focus only on very simple things. Things that could and should be discovered by code analysis tools, IDE inspections, SonarQube and other automatic quality gates. Reviewer’s time is expensive, code analysis tools are cheap.
  • When PR is submitted for the review, the code is already written. It is often too late to comment on the fundamental problems in the design. Such comments are hard to address and naturally, they are met with lots of resistance and even anger from the code author.
  • Often there is a tight deadline to deliver the feature, so the pressure goes on the reviewer – “It is his fault we didn’t deliver the fix on time, he didn’t timely approve my code change”.
  • Typically author of the PR selects the reviewers. This leads to uneven distribution of the reviews (which consume significant time) or sometimes it promotes selecting reviewers that “go easy on everyone”.

What’s the solution?

I propose using the following process:

  1. Developer starts working on the assigned feature/ticket, e.g. TICKET-123.
  2. Create and check out feature branch – feature/t123/my-feature.
  3. Analyze the feature and if it is complex enough write a design document. Depending on the complexity, this could be just a few sentences in the MD file or full wiki page with diagrams, etc.
  4. Create another branch feature/t123/my-feature-design, commit there your design document (or link to it) and submits the PR with overall feature/t123/my-feature as a target. Some projects might require that only tech leads or architects could review such “design” PRs.
  5. Once “Design PR” is approved, merge it and creates another branch feature/t123/my-feature-blueprint. The purpose of this branch is to create the overall skeleton of the feature, adding lots of placeholders for every implementation detail. The more placeholders you add the better it is. Each such placeholder becomes a subtask of the feature. These placeholders might use some special markers like // PH-1 User input validation, and your IDE can be easily configured to display the consolidated list of such markers. After this is done, submit the PR to merge feature/t123/my-feature-blueprint into feature/t123/my-feature. Here your reviewers will have an opportunity to evaluate the overall feature design at the code level and express their concerns or suggestions.
  6. Next, implement every placeholder defined in the previous step. Each placeholder is implemented in a separate branch that is forked from feature/t123/my-feature and dedicated PR is submitted for each. Note that at this stage “placeholder” branches could be implemented in parallel by several developers. Ideally, reviewers of these placeholder implementation PRs should be assigned randomly, promoting a balanced workload as well as knowledge sharing within the development team.
  7. Once all placeholders are resolved, submit the final PR merging the overall feature/t123/my-feature into master. Since every change in this PR is already reviewed and approved, the goal of this PR is to simply verify the commit history of the feature/t123/my-feature branch and make sure no direct code commits are done.
  8. Finally, merge the feature branch into the master.

This process has many advantages. First of all, reviewers have a chance to comment on feature design before too many resources are spent on implementation. Secondly, instead of one huge PR, we are dealing with several small PRs each one staying at a single level of abstraction and never mixing the overall design with small details of a particular step. Each such PR is easy to understand and provide meaningful comments. It promotes knowledge sharing in the team and minimizes frustration and conflicts when some big PRs are rejected.

As usual comments, opinions, and criticism are more than welcome!

Written by isaaclevin

November 13, 2019 at 1:40 pm

Posted in General, Productivity

Identifying technical debt

leave a comment »

Today’s very short post is about ways to identify technical debt and how to find components/classes that require immediate attention.

  1. Files that have a large number of git commits (they are changed often). These files are dependency magnets, they most likely violate “Open-close principle” and “Single responsibility principle”. Create a report by running:
    # list repo files with their total number of commits
    git log --name-only --pretty=format: | sort | uniq -c | sort -nr > git_stats.txt
  2. SonarQube provides pretty good reports showing the complexity and test coverage of your classes

Written by isaaclevin

November 8, 2019 at 7:56 pm

Posted in Java

Properties of good tests

with 2 comments

I recently read a very interesting article called “Test Desiderata” published by Kent Beck. In this article, Kent describes the properties of good tests. He also mentions that tests of different levels (unit, integration, acceptance, etc.) should focus on different properties.

Here I’d like to discuss some of these properties and try to find some practical steps to improve our tests in regard to these properties.

Kent Beck says:

“Programmer (aka “unit” tests). Give up tests being predictive and inspiring for being writable, fast, and specific.”

Writable — tests should be cheap to write relative to the cost of the code being tested.

So, unit tests are our first line of defense. They should be extremely cheap to write, they should be easy to read and they must be very fast to execute.

Tools that help to increase the development speed of such tests are Spock Framework and Groovy in general. Even if the production code is in Java, I still prefer writing my unit tests in Groovy. “Optional typing”, “Multiline strings“, “Safe Navigation“, “Native syntax for data structures” and especially “Initializing beans with named parameters” are huge productivity boosters. Check out my presentation deck for a very basic introduction to Groovy. Spock takes productivity even further allowing you to write very concise and expressive tests. My favorite feature is Spock’s own mock/stub framework. It is way more readable than more commonly used Mockito. Enabling Groovy and Spock in Java projects is simply a matter of adding an appropriate Gradle or Maven dependency.

Here is somewhat controversial thought… If our tests are very cheap to write, maybe we should treat them as immutable. I recently saw a tweet suggesting that we should be allowed to write new unit tests or delete existing ones, never modify them. This might sound a bit extreme, but there is a rationale in it. Quite often our tests look good initially, but their quality and readability degrade with time. We refactor production code, which leads to tests failures, we realize that the test is outdated and we start quickly “patching” it.

Kent Beck says:

Inspiring — passing the tests should inspire confidence.

Structure-insensitive — tests should not change their result if the structure of the code changes.

Predictive — if the tests all pass, then the code under test should be suitable for production.

These properties are very important for integration/end-to-end tests. I discussed this in my previous post. When writing “stable” e2e tests, I use real components/collaborators for the entire system and mock only “edge” dependencies such as DAO Repositories, JMS abstraction interfaces, REST clients, etc. I prefer creating dedicated mock implementations of such “edge” components rather than using raw Mockito, JMock, Spock. This helps to make such tests readable for non-programmers. Typical e2e test starts specially configured application context, invokes input component and validates expected behavior at all “edges”. The value of such tests is enormous, they give us the confidence that the entire system is working properly. Being structure-insensitive, e2e tests serve us as safety-net when doing code refactoring.

In one of the future posts, I’ll cover my experience working with legacy code and how to efficiently create e2e tests there.

As usual comments, opinions, and criticism are more than welcome!



Written by isaaclevin

November 3, 2019 at 10:54 am

Posted in Java, Testing

Don’t over spec your tests

with 3 comments

Yesterday I had a discussion with my colleagues about the properties of good tests. I think in general tests have 4 purposes in the following increasing order of importance:
  1. Validate correctness of the system under test
  2. Document usage and expectations of the tested module
  3. Help designing component’s API and interactions (when practicing TDD)
  4. Provide a safety net that enables fearless refactoring
The last point is the most important in my opinion. To provide such safety net, tests must be, as stated by Kent Beck, “sensitive to changes in system behavior, but insensitive to changes in code structure”.
How to achieve this?
Perhaps we should prefer higher-level component/module tests. Such tests are quite more stable and insensitive to structural changes. We should limit the usage of mocks in such tests, probably only mocking collaborators that live outside of the component boundaries.
We should only verify interactions with collaborators that are essential to the business logic of our component.
What do I mean by that? Often I see unit tests where developers stub responses of component collaborators and then verify ALL these interactions. With Mockito they sometimes utilize “lenient” matchers like any() or isA() while stubbing; and “strict” matches like eq() while verifying. This technique is OK, but in my opinion, it should only be applied to true mocks – calls that are essential to the behavior of the system.
Calls to simple data providers (stubs) shouldn’t be verified at all, otherwise, it delivers wrong intentions of the code author as well as makes tests quite fragile.
The difference between stubs and mocks is greatly explained in this article by Martin Fowler.
What do you think? How do you make your tests insensitive to structural changes?

Written by isaaclevin

October 19, 2019 at 11:10 am

Posted in Java, Testing

Project release and version management using gradle and git

leave a comment »

Two very useful Gradle plugins:

1. axion-release-plugin

This plugin derives project version from tags in git repository. I use following rules in my projects:

  • Start with default version 0.1.0-SNAPSHOT
  • Every release of master branch increments second digit
  • Production emergency fixes are done on a branch, which is created from the version tag. Such branch name must start with “hotfix-” prefix. Releasing this branch will increment 3rd digit.
  • Feature branches must have “topic-” prefix. They are never released, however their version includes feature name.

To check current project version run “gradle currentVersion”
To release the project run “gradle release”

2. gradle-nexus-plugin

This plugin adds ability to upload project artifacts to nexus repository. Simply run “gradle upload”.


buildscript {
    repositories {
        maven {url = ''}

plugins {
    id 'java'
    id '' version '1.3.4'
    id '' version '2.3.1'

scmVersion {
    versionIncrementer 'incrementMinor'
    branchVersionIncrementer = [
            'hotfix-.*' : { c -> c.currentVersion.incrementPatchVersion() }
    branchVersionCreator = [
            'topic-.*' : 'versionWithBranch'

group 'com.jpragma.myproject'
version = scmVersion.version

repositories {
    jcenter {url = ''}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'

nexus {
    sign = false
    repositoryUrl = 'http://localhost:8081/nexus/content/repositories/internal/'
    snapshotRepositoryUrl = 'http://localhost:8081/nexus/content/repositories/internal-snapshots/'

Written by isaaclevin

July 12, 2016 at 2:03 pm

Posted in Uncategorized

Useful aliases and ENVs in cygwin .profile

leave a comment »

alias cp=’cp -i’
alias mv=’mv -i’
alias df=’df -h’
alias du=’du -h’
alias grep=’grep –color’
alias itest=’mvn clean test verify -U’
alias ls=’ls -h –color’
alias mci=’mvn clean install -U’
alias mi=’mvn install’
alias mjr=’mvn jetty:run -o’
alias mjrwithprofile=’mvn clean jetty:run -DAPP_ENV=dev’
alias ps=’ps -W -a -f ux’
alias rm=’rm -i’

function xtitle {
echo -ne “\033]0;$1\007″

export MAVEN_OPTS_BASE=”-server -Xms128m -Xmx2048m -XX:MaxPermSize=256m”
export MAVEN_OPTS_DEBUG=”$MAVEN_OPTS_BASE -Xdebug=true -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=5555,server=y,suspend=n″
alias mvnAllOff=’export MAVEN_OPTS=$MAVEN_OPTS_BASE’
alias mvnDebugOn=’export MAVEN_OPTS=$MAVEN_OPTS_DEBUG’


Written by isaaclevin

May 11, 2016 at 8:45 am

Posted in Uncategorized

Useful options running “mvn test”

with one comment

There are some very useful command line options you can specify when executing maven tests:

-Dtest=[testname], this allows selective execution of unit tests, instead of running them all. [testname] might be full class name, class name without package, or even wildcard.

-Dsurefire.useFile=false, this will force surefire plugin to write failing test info to stdout instead of file in target/surefire-reports/. Can be a huge time saver.

Written by isaaclevin

January 28, 2011 at 12:58 pm

Posted in Java

Control Log4j message timezone

leave a comment »

If your server is running in one timezone, but you want to have log messages be printed using different timezone, here is a simple solution:

  1. Add log4j extras to project classpath (maven GAV is log4j:apache-log4j-extras:1.0)
  2. In log4j.xml use EnhancedPatternLayout, specifying required timezone for logging purposes (see example below for EST TZ)
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="TRACE" />
    <layout class="org.apache.log4j.EnhancedPatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601}{EST} %-5p [%t][%c:%M(%L)] %m%n" />

In this example, log messages will be printed using EST regardless of your server timezone.

Written by isaaclevin

December 27, 2010 at 3:23 pm

Posted in Java