0

I am writing Junit for my Spring 4 application where I am using test.properties file from src/test/resources directory for test properties as below

<context:property-placeholder location="classpath:test.properties" />

Below is my Junit test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-servlet.xml")
public class MyHandlerTest {

}

In my application I have classes which has @Service annotation as below

@Service
public class MyHandler {
  @Inject
  Environment env;
  //rest of the code
}

When in my handler I am trying to get property as

env.getProperty("someProperty");

I am getting null value here, so my problem is how I can access properties from all my classes?

Update

I got confuse, actually null pointer is because of it is not Autowring the interface, I have

@Service
public class MyHandler {
  @Inject
  Environment env;

  @Autowired
  Util util;

  //rest of the code
}

Where I am getting null for util, can somebody let me know how Autowired interface works in Junit, from spring-servlet.xml file I am scanning all packages but still Autowire is null.

ppb
  • 2,299
  • 4
  • 43
  • 75
  • try with this, `@PropertySource("classpath:test.properties")` – Arvind Katte Dec 28 '17 at 07:53
  • If your project structure is `maven` then keep `test.properties` file in, something like this `src/main/resources/config/test.properties` – Arvind Katte Dec 28 '17 at 07:56
  • Possible duplicate of [Spring jUnit Testing properties file](https://stackoverflow.com/questions/32974432/spring-junit-testing-properties-file) also https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test – tkruse Dec 28 '17 at 08:30
  • You should add `PropertySourcesPlaceholderConfigurer` bean also. – Hadi J Dec 28 '17 at 10:11

0 Answers0