【问题标题】:How do you mock Environment interface?你如何模拟环境界面?
【发布时间】:2013-11-16 12:05:26
【问题描述】:

我正在尝试测试我的服务,它看起来像:

import org.springframework.core.env.Environment;

@Service
public class MyService {
@Autowired Environment env;
...
...
}

如何模拟环境接口,或者如何创建一个?

【问题讨论】:

    标签: spring mocking spring-test


    【解决方案1】:

    Spring 为属性源和环境提供模拟。这两个都可以在spring-test 模块的org.springframework.mock.env 包中找到。

    • MockPropertySource:自 Spring Framework 3.1 起可用 -- Javadoc
    • MockEnvironment:自 Spring Framework 3.2 起可用 -- Javadoc

    这些在测试章节的Mock Objects 部分的参考手册中有简要记录。

    问候,

    山姆

    【讨论】:

      【解决方案2】:

      使用 Mockito,您应该能够像下面的代码那样完成它。请注意,您需要提供访问器,以便您可以在运行时设置 Environment 字段。或者,如果您只有几个自动装配的字段,那么定义一个可以注入 Environment 的构造函数会更简洁。

      import static org.mockito.Mockito.when;
      import static org.mockito.MockitoAnnotations.initMocks;
      import org.junit.Before;
      import org.junit.Test;
      import org.mockito.Mock;
      
      public class MyServicetest {
      
          // Define the Environment as a Mockito mock object
          @Mock Environment env;
      
          MyService myService;
      
          @Before
          public void init() {
              // Boilerplate for initialising mocks 
              initMocks();
      
              // Define how your mock object should behave
              when(this.env.getProperty("MyProp")).thenReturn("MyValue");
      
              // Initialise your service
              myService = new MyServiceImpl();
      
              // Ensure that your service uses your mock environment
              myService.setEnvironment(this.env);
          }
      
          @Test
          public void shouldDoSomething() {
              // your test
          }
      
      }
      

      【讨论】:

      • 如果我不想模仿环境怎么办,我想使用现有的ebv,以及所有现有的属性,我不想重写所有的属性
      • erm ...您确实问过如何模拟环境...:-/
      • 这对我有用,应该是正确的答案。必须先initMocks()
      【解决方案3】:

      实现类具有@Autowired Environment env;因此,当您运行 >JUnit 测试用例时,您的实现类应该具有如下构造函数:

      public class SampleImpl{
      @Autowired
      Environment env
      public SampleImpl(Environment envObj){
      this.env = envObj}
      }
      

      您的 Junit Test 类应如下所示:

      import static org.junit.Assert.*;
      import static org.mockito.Mockito.*;
      import static org.mockito.MockitoAnnotations.initMocks;
      import org.junit.Before;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.mockito.Mock;
      import org.springframework.core.env.Environment;
      import static org.mockito.Mockito.when;
      public class SampleTest {   
      
           @Mock Environment env;
      
           @Before
            public void init(){
                env = mock(Environment.class);     
                when(env.getProperty("file.location"))
                  .thenReturn("C:\\file\\");        
            }
      
                @Test
                public void testCall()throws Exception{
                    SampleImpl obj = new SampleImpl(env);
                    obj.yourBusinessMethods();
      
                }
      }
      

      希望这会有所帮助。谢谢史蒂夫。

      【讨论】:

      • 我试过了,但它不起作用。我声明的 env 变量为 null。
      【解决方案4】:

      在基于 Spring 的测试中,您可以使用:@ActiveProfiles 所以激活一些配置文件(但这不是模拟)

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration("test.xml")
      @ActiveProfiles("myProfile")
      public class ProfileTest {
      
        @Autowired
        MyService myService
      
        @Test
        public void demo() {
            assertTrue(myService.env.acceptsProfiles("myProfile"));
        }
      }
      

      但我需要一个模拟,然后编写自己的模拟框架或使用模拟框架(Mokito 或 JMock)。 Environment 有一个子类AbstractEnvironment,你只需要覆盖customizePropertySources(MutablePropertySources propertySources) 方法

      @Override
      protected void customizePropertySources(MutablePropertySources propertySources) {
                  Properties properties = ....
          propertySources.addLast(new MockPropertySource(properties));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-09
        • 2011-09-11
        相关资源
        最近更新 更多