javaspringspring-boottestingjunit

Java Spring Boot: Test for utility class not run


I am very sorry if this question has already been asked but I could not find any answer for it.

In my Spring Boot application, I have a utility class that I use on several places in different services. The util - located in src/main/java/com/blueprint/bookings/util/AccountUtil.java - looks like this:

package com.blueprint.bookings.util;

... IMPORTS ...

public final class AccountUtil {
    private AccountUtil() throws UnsupportedOperationException {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    public static Account getAccountFromRequestDto(RequestDto requestData) {
        ... CODE ...
    }

    public static UserEntity getUserEntityFromAccount(Account account) {
        ... CODE ...
    }
}

As I stated earlier I, this utility class and it's methods is used in several @Service classes, that is why I have created an utility for it.

Note that the utility class does not have any annotations. I tried annotating using both @Component and @Service but that just made all other (currently only controller) tests fail.

I want to write unit tests for this class and the methods. This is what I have done so far - located in src/test/java/com/blueprint/bookings/util/AccountUtilTest.java:

package com.blueprint.bookings.util;

... LOCAL IMPORTS ...
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AccountUtilTest {

    @Test
    void testGetAccountFromRequestDto() throws Exception {
        ... CREATE TEST OBJECT "testRequestDto" ...

        Account resultAccount = AccountUtil.getAccountFromRequestDto(testRequestDto);

        ... ASSERTIONS WITH "assertEquals" ...
    }

    @Test
    void testGetUserEntityFromAccount() throws Exception {
        ... CREATE TEST OBJECT "testAccount" ...

        UserEntity resultAccount = AccountUtil.getUserEntityFromAccount(testAccount);

        ... ASSERTIONS WITH "assertEquals" ...
    }
}

My problem here is that when I run mvn test or mvn install it does not get run at all. I see that other tests for my controllers and services are run but not this test.

I tried removing all tests but these, and then I see in the console, also indicating that these tests does not get run.

[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

What might I be missing here?


Solution

  • As AccountUtil is a utility class with static methods, it doesn’t need the Spring context to be loaded. Instead, it seems like a simple JUnit test class.

    Here is the sample code snippet shown below through JUnit 5

    class AccountUtilTest {
    
        @Test
        void testGetAccountFromRequestDto() {
            ... CREATE TEST OBJECT "testRequestDto" ...
    
            Account resultAccount = AccountUtil.getAccountFromRequestDto(testRequestDto);
    
            ... ASSERTIONS WITH "assertEquals" ...
        }
    
    }