简体   繁体   English

使用Mockito测试Spring环境配置文件

[英]Testing Spring Environment Profiles with Mockito

The latest version of Spring Framework has deprecated Environment.acceptsProfiles(String ...) in favour of Environment.acceptsProfiles(Profiles ...) Spring Framework的最新版本不推荐使用Environment.acceptsProfiles(String ...) ,而推荐使用Environment.acceptsProfiles(Profiles ...)

Updating this in one of my applications has made testing more difficult, here is some test code to demonstrate the issue: 在我的一个应用程序中更新它使测试变得更加困难,下面是一些测试代码来演示该问题:

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.core.env.StandardEnvironment;

public class EnvironmentProfilesTest {

    @Test
    public void testItWithRealEnvironment() {
        System.setProperty(StandardEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "adrian");
        Environment environment = new org.springframework.core.env.StandardEnvironment();

        ToBeTested toBeTested = new ToBeTested(environment);
        assertTrue(toBeTested.hello("adrian"));
    }

    @Test
    public void testItWithMocklEnvironment() {
        Environment environment = mock(Environment.class);
when(environment.acceptsProfiles(Profiles.of("adrian"))).thenReturn(true);

        ToBeTested toBeTested = new ToBeTested(environment);
        assertTrue(toBeTested.hello("adrian"));
    }

    private static class ToBeTested {
        private Environment env;
        public ToBeTested(Environment env) {
            this.env = env;
        }

        public boolean hello(String s) {
            return env.acceptsProfiles(Profiles.of(s));
        }
    }
}

The old version using a String argument to acceptsProfiles was easy to mock. 使用String参数接受acceptsProfiles的旧版本很容易模拟。 What am I doing wrong? 我究竟做错了什么? It feels like the Profiles class might benefit an equals() method? 感觉Profiles类可能会受益于equals()方法?

You may create a simple matcher based on the toString . 您可以基于toString创建一个简单的匹配器。

public static Profiles profiles(String... profiles) {
    return argThat(argument -> {
        String expected = Objects.toString(Profiles.of(profiles));
        String actual = Objects.toString(argument);
        return expected.equals(actual);
    });
}

Then you use the matcher as follows: 然后使用匹配器,如下所示:

when(environment.acceptsProfiles(profiles("myProfile"))).thenReturn(true);

It isn't Spring, it's just incorrect approach. 不是Spring,这只是错误的方法。 As I can see the problem is in this part of code: when(environment.acceptsProfiles(Profiles.of("adrian"))).thenReturn(true); 如我所见,问题出在这部分代码中: when(environment.acceptsProfiles(Profiles.of("adrian"))).thenReturn(true);

You use mock for Environment and try to catch instance of Profiles class, something like: .acceptsProfiles(eq(Profiles.of("adrian"))) . 您对Environment使用模拟,并尝试捕获Profiles类的实例,例如: .acceptsProfiles(eq(Profiles.of("adrian"))) You can't catch it because you create another instance in the method boolean hello(String s) and Environment never returns true. 您无法捕获它,因为您在boolean hello(String s)方法中创建了另一个实例,而Environment从不返回true。

You just described incorrect behavior for the mock Environment and you can fix it: 您刚刚描述了模拟Environment不正确行为,可以对其进行修复:

put any any

@Test
    public void testItWithMocklEnvironment() {
        Environment environment = mock(Environment.class);
        when(environment.acceptsProfiles(any(Profiles.class))).thenReturn(true);

        ToBeTested toBeTested = new ToBeTested(environment);
        assertTrue(toBeTested.hello("adrian"));
    }

or don't use mock (I think this is what you're looking for): 不使用模拟 (我认为这是您要寻找的):

@Test
    public void testItWithMocklEnvironment() {
        Environment environment = new org.springframework.core.env.StandardEnvironment();
        ((StandardEnvironment) environment).setActiveProfiles("adrian");

        ToBeTested toBeTested = new ToBeTested(environment);
        assertTrue(toBeTested.hello("adrian"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM