Skip to content
Advertisement

How to @Autowired BuildProperties in test?

I am using this annotation to display the build version in my app:

JavaScript

With this maven plugin:

JavaScript

It works well in the app, but prevent my test to run.

Here is the test class config:

JavaScript

I add the file src/test/resources/META-INF/build-info.properties

But the build still have errors:

JavaScript

How can I make this works during test ?

Solution: add the annotation @SpringBootTest

Advertisement

Answer

Whatever you autowire in your class under test has to be set in the test class. One way to do this is using ReflectionTestUtils.

JavaScript

here objBuildProperties is an object of BuildProperties that you can instantiate and use in the line of code above.

I would also ask you, when exactly do you get this error ? While running a test case or when ?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement