How do you mix the environment interface?

I am trying to check my service, which looks like this:

import org.springframework.core.env.Environment; @Service public class MyService { @Autowired Environment env; ... ... } 

How do I trick the environment interface, or how to create it?

+7
spring spring-test mocking
source share
4 answers

Spring provides mocks for property sources and environments. Both of them can be found in the org.springframework.mock.env package of the spring-test module.

  • MockPropertySource : Available with Spring Framework 3.1 - Javadoc
  • MockEnvironment : Available with Spring Framework 3.2 - Javadoc

They are briefly described in the reference guide in the Mock Objects section of the testing chapter.

Hi,

Sam

+11
source share

The implementation class has @Autowired Environment env; Therefore, when you run the JUnit test case, the implementation class should have such a constructor as shown below:

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

your Junit Test class should be as follows:

 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.

+3
source share

In the Spring test, you can use: @ActiveProfiles to activate some profile (but this is not a layout)

 @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 need a layout, then write your own or use a mocking structure (Mokito or JMock). Environment has an AbstractEnvironment sublayer, where you just need to override the customizePropertySources(MutablePropertySources propertySources) method

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

Using Mockito, you can do this as the code below. Note that you need to either provide accessors so that you can set the Environment field at run time. Alternatively, if you have only a few auto-programming fields, it may be easier to define a constructor into which you can enter the 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 } } 
0
source share

All Articles