mardi 9 février 2021

Spring bean methods don't use the field value injected by ReflectionTestUtils.setField

I have a spring project, which contains a class called EnvUtils:

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class EnvUtils {

    @Value("${env}")
    private String env;

    /**
     * Develop environment
     */
    public boolean isDev() {
        return Objects.equals(env, "dev");
    }

    /**
     * Production environment
     */
    public boolean isProd() {
        return Objects.equals(env, "prod");
    }

}

I wrote a simple test, which set env's value to dev, and checked the result of isDev():

package com.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.util.ReflectionTestUtils;

@SpringBootTest
@ExtendWith(MockitoExtension.class)
class MainControllerTests {

    @MockBean
    private EnvUtils envUtils;

    @Test
    void fieldTest() {
        ReflectionTestUtils.setField(envUtils, "env", "dev");
        Assertions.assertTrue(envUtils.isDev());
    }

}

The test should pass because the value of env is dev, which means envUtils.isDev() should definitely return true. However, this test failed. I debugged it and found that envUtils.env was dev, as expected, but envUtils.isDev() returned false. How could it be? Have I done something wrong?

Aucun commentaire:

Enregistrer un commentaire