spring 你如何模拟环境接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19791984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you mock Environment interface?
提问by storm_buster
I am trying to test my service which looks like :
我正在尝试测试我的服务,它看起来像:
import org.springframework.core.env.Environment;
@Service
public class MyService {
@Autowired Environment env;
...
...
}
How do I mock Environment Interface, or else how do I create one?
我如何模拟环境接口,或者我如何创建一个?
回答by Sam Brannen
Spring provides mocks for property sources and the environment. Both of these can be found in the org.springframework.mock.env
package of the spring-test
module.
Spring 为属性源和环境提供模拟。这两个都可以org.springframework.mock.env
在spring-test
模块的包中找到。
MockPropertySource
: available since Spring Framework 3.1 -- JavadocMockEnvironment
: available since Spring Framework 3.2 -- Javadoc
MockPropertySource
:自 Spring Framework 3.1 起可用 -- JavadocMockEnvironment
:自 Spring Framework 3.2 起可用 -- Javadoc
These are briefly documented in the reference manual in the Mock Objectssection of the testing chapter.
这些在测试章节的模拟对象部分的参考手册中进行了简要记录。
Regards,
问候,
Sam
山姆
回答by KarthikDev
Implementation Class has @Autowired Environment env; So when you are running >JUnit test case you implementation class should have a constructor like below:
实现类具有@Autowired Environment env;因此,当您运行 >JUnit 测试用例时,您的实现类应该具有如下构造函数:
public class SampleImpl{
@Autowired
Environment env
public SampleImpl(Environment envObj){
this.env = envObj}
}
your Junit Test class should be as follows:
您的 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();
}
}
Hope this helps. Thanks Steve.
希望这可以帮助。谢谢史蒂夫。
回答by Steve
Using Mockito, you should be able to do it somewhat like the code below. Note that you need to either provide accessors so that you can set the Environment field at runtime. Alternatively, if you only have a couple of autowired fields, it can be cleaner to define a constructor where you can inject the Environment.
使用 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
}
}
回答by Ralph
In a Spring based test you can use: @ActiveProfiles
so activate some Profile (but this is not a mock)
在基于 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"));
}
}
But I you need a mock then write your own or use a mocking framework (Mokito or JMock). Environment
has an sublass AbstractEnvironment
, where you just need to override customizePropertySources(MutablePropertySources propertySources)
methode
但是我需要一个模拟然后编写自己的或使用模拟框架(Mokito 或 JMock)。Environment
有一个 sublass AbstractEnvironment
,你只需要覆盖customizePropertySources(MutablePropertySources propertySources)
methode
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
Properties properties = ....
propertySources.addLast(new MockPropertySource(properties));
}