Category Archives: Java - Page 7

Spring Boot: Create a “RunAllTests” test class with JUnit 5

I like having an easy way to run all tests in a specific package path. I will here show an example of how to create a “RunAllTests” class to use in Spring Boot 2 and JUnit 5 (Jupiter):

package com.niklasottosson.myapp;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;

/**
 * Test class to run all unit tests.
 */
@RunWith(JUnitPlatform.class)
@SelectPackages("com.niklasottosson.myapp.tests")
public class RunAllTests {
}

This will tell JUnit to search the files in the package described (and below) in the @SelectPackages annotation for tests to run

For Spring Boot 2.3.8 the normal “spring-boot-starter-test” dependency was not enough to get the JUnitPlatform into my app (for the @RunWith annotation), so I had to add the following dependency to my pom.xml:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>

After all is in place I can run the RunAllUnitTests class and all selected tests runs

Tested on Spring Boot v2.3.8, IntelliJ v2020.3 and JUnit v5 with JUnitPlattform v1.2.0

Mock nested method calls using Mockito in Spring Boot

Quite often you have a nested call chain to get a result in Java. Mocking these in a test is a little different than mocking just one call. I’m here going to show a solution to that.

Say you have the following call chain to create a transacted session for a JMS connection:

Session session = 
           connectionFactory.createConnection()
                            .createSession(Session.SESSION_TRANSACTED);

To mock these calls we have to break them apart and mock them one by one

    
...
public MyTestClass {
  @Mock
  private Session session;

  @Mock
  private ConnectionFactory connectionFactory;

  @Mock
  private Connection mockedConnection;

  @BeforeMethod
  public void setUp() throws JMSException {
    MockitoAnnotations.initMocks(this);
    Mockito.when(connectionFactory.createConnection())
                                          .thenReturn(mockedConnection);
    Mockito.when(mockedConnection.createSession(
                                           Session.SESSION_TRANSACTED))
                                          .thenReturn(session);
  }
...

We are now ready to create all our tests 🙂

Tested on Java v1.8.0_252, Mockito v3.3.3, Spring Boot v2.2.4 and TestNG v6.11

Using a different Bean depending on profile in Spring Boot

This is something that can be very useful when running your application in different environments. One case is that you want to place messages on a queue but you do not have access to the queue manager in the development environment. Here is an example of what a solution might be to that problem:

First, we create an interface that can act as a facade for our “send” method

public interface Gateway {
  void send(Message message);
}

Now we need to add this interface to our class

public class MQGateway implements Gateway {
  @Override
  void send(Message message){
    /* All the logic needed to send a message */
  }
}

And now we create two Beans. One for each profile

@Bean
@Profile("dev")
public MQGateway mqgatewayDevelop() {
  return new MQGateway() {
    @Override
    void send(Message message) {
      log.info("Mocked gateway. Sending message to log: " + message);
    }
  }
}

@Bean
@Profile("prod")
public MQGateway mqgatewayProduction() {
  return new MQGateway();
}

I usually put Bean creation in a configuration class so that any setting from application.yml is close at hand

And now we are going to use some “magic” to make Spring choose the right one

public class MyClass {
  @Autowire
  private Gateway mqGateway;

  public void myFunction(Message message) {
    mqGateway.send(message);
  }
}

So, how does this work. Well, by using the interface when calling the ‘send’ method we are forcing Sprint to find a suitable Bean of that type and since there only will be one active per profile Spring will find the right one.
If Spring can’t find a suitable Bean it will throw a exception

Tested on Spring Boot v2.2.4