1

I have the following Controller class

@Component
public class Controller{
@Value("${details}")
private String detail1;
@Value("${otherdetails}")
private String detail2;
public ModelClass getSomeDetails(String name, int age) {
 Serviceclass serviceobject = new Serviceclass();
 //some Code
 serviceobject.doSomething(name, age, detail1, detail2);
 }
}

Once this is done, I need to test this method in a test case. My test class is as below. I have the application.properties file in the /src/test/resources folder

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations="classpath:application.properties")
public class testClass{
@Test
public void getDetails() {
Controller controllerObject = new Controller();
controllerObject.getSomeDetails("name", 22);
//Other code
 }
}

When I do this, the application.properties has the details and otherdetails parameter but it is returning null.

How should I use them to get the value in the Controller class?

asked Sep 10, 2019 at 6:38
3
  • src\main\resources\application.properties is the structure right?? Commented Sep 10, 2019 at 6:50
  • Yes, I have the file in that location as well. since that did not work, I have over-ridden the properties file by creating a new file under the above mentioned location Commented Sep 10, 2019 at 6:52
  • @rjdkolb , @TestPropertySource(locations="classpath:application.properties") should be fine. Commented Sep 10, 2019 at 6:55

2 Answers 2

1

Do not manually instantiate the controller, rather AutoWire the controller into your test class.

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations="classpath:application.properties")
public class testClass{
@Autowired Controller controllerObject;
@Test
public void getDetails() {
controllerObject.getSomeDetails("name", 22);
//Other code
 }
}
answered Sep 10, 2019 at 7:22
Sign up to request clarification or add additional context in comments.

1 Comment

Doing this was not successful either. I ended up with UnsatisfiedDependencyException where error creating bean with name "...controller.testClass"
1

You need to let Spring instantiate your Controller class and not manually do a new yourself:

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations="classpath:application.properties")
public class testClass{
 @Autowired
 private Controller controllerObject;
 @Test
 public void getDetails() {
 controllerObject.getSomeDetails("name", 22);
 //Other code
 }
 @TestConfiguration
 static class TestConfig {
 @Bean
 public Controller controller() {
 return new Controller();
 }
 }
}

Note that you can also replace @RunWith(SpringJUnit4ClassRunner.class) with @RunWith(SpringRunner.class).

Instead of adding the inner class, you can also have Spring test start your complete application by annotating the test with @SpringBootTest. If you know what part of your application you want to test, you can get better performance by using test slicing.

answered Sep 10, 2019 at 7:20

3 Comments

Doing this was not successful either. I ended up with UnsatisfiedDependencyException where error creating bean with name "...controller.testClass"
The above is error free with adding @SpringBootTest, but I still get null for the values from the property files
@evan I suggest you create a sample application to further investigate the issue. You can also manually configure the values to use in the test with @TestPropertySource(properties="details=x"). See baeldung.com/spring-test-property-source

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.