17

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

import org.springframework.core.env.Environment;

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

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

4

4 回答 4

24

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

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

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

问候,

山姆

于 2013-11-05T21:41:51.503 回答
4

使用 Mockito,您应该能够像下面的代码一样做到这一点。请注意,您需要提供访问器,以便您可以在运行时设置 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
    }

}
于 2013-11-05T17:39:25.243 回答
2

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

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

您的 Junit 测试类应如下所示:

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();

          }
}

希望这可以帮助。谢谢史蒂夫。

于 2016-04-26T08:26:43.897 回答
1

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

@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有一个 sublass AbstractEnvironment,你只需要覆盖customizePropertySources(MutablePropertySources propertySources)methode

@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
            Properties properties = ....
    propertySources.addLast(new MockPropertySource(properties));
}
于 2013-11-05T15:12:33.427 回答