Dinesh Arora Dinesh Arora - 1 month ago 16
Java Question

Spring 4 application.properties @Value is not set when running Junit test case

I am using @value("${}") to read a property from application.properties file from src/main/resources folder. This works well, when I run the application. However when I run a test case, this value is not set.
I have already tried the following:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")


Here's my complete code.

Application.java

@SpringBootApplication
@ComponentScan ({"acn.*"})
public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {

return new PropertySourcesPlaceholderConfigurer();
}

}


EntApiUtil.java This is where @value is set

@Component
public class EntApiUtil implements IEntApiUtil{
Logger log = LoggerFactory.getLogger(this.getClass());

@Value("${entapi.url}")
private String entApiUrl;
private static final String STATUS_VALID = "1";
private static final String STATUS_INVALID ="0" ;
private static final String STATUS_ERROR ="0" ;
private static final int TIMEOUT = 60000;
private static final String SERVICE_NAME_ADDRESS = "AddressService";
..............
..............

private AddressBindingStub getAddressBindingStub() throws Exception {
AddressBindingStub binding = null;
try {
AddressServiceLocator addressServiceLocator = new AddressServiceLocator();
System.out.println("Ent-API URL: " + entApiUrl);
addressServiceLocator.setAddressPortEndpointAddress(entApiUrl + "/" + SERVICE_NAME_ADDRESS);
binding = (AddressBindingStub) addressServiceLocator.getAddressPort();
//Time out after a minute
binding.setTimeout(TIMEOUT);
} catch (javax.xml.rpc.ServiceException jre) {
log.error("Error occurred attempting to establish address validation API.", jre);
System.out.println(" ERROR");
}

return binding;
}


TEST CLASS

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")
public class EntApiUtilTest {


IEntApiUtil entApiUtil;
Address address;
StatusVO statusVO;

@Before
public void setUp(){
entApiUtil = new EntApiUtil();
address = new Address();
address.setStAddress("2120 Mission Rd");
address.setUnitNum("250");
address.setUnitType("Suite");
address.setCity("ESCCONDIDO");
address.setState("CA");
address.setZipcode("92029");
}

@Test
public void validateAddress_statusVO_not_null(){
statusVO = entApiUtil.validateAddress(address);
assertNotNull(statusVO);
}

@Test
public void validateAddress_valid_streetName(){
statusVO = entApiUtil.validateAddress(address);
System.out.println(statusVO.getValidatedAddress().getStreetName() + " =======");
assertTrue("Valid St reet Name", statusVO.getValidatedAddress().getStreetName().equalsIgnoreCase("Mission"));
}
}


I get Nullpointer exception because value is not being set when I run test cases. I have used all the annotations possible as evident in the classes and also tried adding this to my Config class Application.java(tried removing it as well)

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {

return new PropertySourcesPlaceholderConfigurer();
}


What am I missing now?

Answer

You dont need to add PropertySourcesPlaceholderConfigurer when you are using SpringBoot, it is already added by default.

why EntApiUtil property values are not set?

you are instantiating new EntApiUtil() by your own, so spring wont know this.

you need to wire this

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource("classpath:/application.properties")
public class EntApiUtilTest

 @Autowire
 private EntApiUtil entApiUtil

   @Test
    public void validateAddress_statusVO_not_null(){
///..address etc
        statusVO =  entApiUtil.validateAddress(address);
        assertNotNull(statusVO);
    }

}
Comments