0

If I activate a Profile in Tests with @ActiveProfiles, say @ActiveProfiles("test"), and declare @Mock private ConfigurableEnvironment environment in test, will I be able to retrieve environment.getProperty("spring.profiles.active"). I am getting the value as null.

Example test is as follows

@RunWith(SpringJUnit4ClassRunner.class)
    @WebMvcTest(value = ProductController.class)
    @ActiveProfiles("test)
    class ProductControllerTest{
        @Autowired
        private MockMvc mockMvc;
       
        @MockBean
        private ProductServiceImpl productServiceImpl;
    
        @Mock
        private ConfigurableEnvironment enviroment;
    
        @Before
        public void setup(){
          MockitoAnnotations.initMocks(this);
        }
    
       @Test
        public void test() throws Exception{
            String profile = environment.getProperty("spring.profiles.active"); // -> This returns null ...why ? If i am setting ActiveProfile as test ..why does above return null?
        }
}
sammy
  • 31
  • 3

1 Answers1

0

The ConfigurableEnvironment environment is being mocked over here, hence you will not be able to retrieve any value using this. Please use the actual bean using:

     @Autowired
    private ConfigurableEnvironment enviroment;

Mockito mocks are used in scenarios when you need to provide a dummy value. Please refer: https://www.baeldung.com/mockito-annotations understand how mocks can be used.

balias
  • 499
  • 1
  • 4
  • 17
  • Hi balias, Thank you for the response. Using actual bean has not worked either. environment.getProperty("spring.profiles.active") is still returning null – sammy Oct 21 '21 at 14:30