0
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@ActiveProfiles(profiles = "test")
@RunWith(MockitoJUnitRunner.class)
public class ConfigurationTest {
 @Autowired
 private Environment environment;
 @Test
 public void test1() throws Exception {
 environment.getProperty("bhavya",Boolean.class);
 }
 @Configuration
 @Profile("test")
 @ComponentScan(basePackages={"com.bhavya.test"})
 public static class EnvironmentServiceTestConfiguration{
 @Bean
 @Primary
 public Environment environment(){
 return Mockito.mock(Environment.class);
 }
 }
}

I also tried putting EnvironmentServiceTestConfiguration as a non-inner non-static class, but didn't help.

Here is what I tried in a separate class:

@RunWith(MockitoJUnitRunner.class)
@Profile("test")
@Configuration
class EnvironmentServiceTestConfiguration{
 @Bean
 @Primary
 public Environment environment(){
 return Mockito.mock(Environment.class)
 }
}

didn't work either

The test class is located in test/java/com.bhavya.test package. I am trying to run this particular test test1

This is my first test of such kind. I have never before used AnnotationConfigContextLoader.class, enlighten me.

Stacktrace :

java.lang.NullPointerException

at Line number where I have statement :

environment.getProperty("bhavya",Boolean.class);
asked Jan 4, 2018 at 5:09
1
  • It is just java.lang.NullPointerException at Line number where I have statement : environment.getProperty("bhavya",Boolean.class); Commented Jan 4, 2018 at 5:27

2 Answers 2

3

Try using @RunWith(SpringJUnit4ClassRunner.class)

Here is an example explaining how can be done https://www.mkyong.com/unittest/junit-spring-integration-example/

Here is a code sample that works for the sample in the question:

package com.example.demo.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigurationTest {
 @Autowired
 private Environment environment;
 @Test
 public void test1() throws Exception {
 System.out.println(environment.getProperty("bhavya",Boolean.class));
 }
 @Configuration
 @ComponentScan(basePackages={"com.example.demo.test"})
 public static class EnvironmentServiceTestConfiguration{
 @Bean
 @Primary
 public Environment environment(){
 Environment env = Mockito.mock(Environment.class);
 when(env.getProperty(eq("bhavya"), eq(Boolean.class))).thenReturn(true);
 return env;
 }
 }
}
answered Jan 4, 2018 at 5:18
Sign up to request clarification or add additional context in comments.

1 Comment

It gives me : org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.bhavya.test.ConfigurationTest': Unsatisfied dependency expressed through method 'setObjectPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor<java.lang.Object>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Why does it tries to create a bean for ConfigurationTest ?
0

Try below annotations for your test class

@ContextConfiguration(classes = Config.class)
@SpringBootTest
@ExtendWith(SpringExtension.class)
@RunWith(SpringJUnit4ClassRunner.class)

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.