-1

I have a spring boot application that reads from an excel document. This is currently being done by a service that my controller uses.

Currently the path of the document is hard coded in the Service class. I would like to know, if this is the best way to do it.

I would also like to know the best practises for unit testing my code. How do I ensure that I have no dependency with the actual file. My helper method is private. I am having troubles mocking it.

sjaymj62
  • 386
  • 2
  • 18

2 Answers2

0

Configuration details should be externalized for both convenience (easy deploy, testing) and security (plain text in your code vs environment variables). Here's a good discussion on the subject.

Mocking is the way to unit-test it. You should be more specific on the problems you have with this and IMHO it would be best to ask that in another question.

MartinBG
  • 1,500
  • 13
  • 22
0

You can provide the path in your "application.properties" file by any variable name.

For example :

my-file=/home/path/to/file.extension

Then, in your service class, declare a variable like :

@Value("${my-file}")
private String filePath;

Now file will have the value which you provide in the application properties. When your application starts, it will automatically binds the path of your file in your variable. In this way, it is easy for you to modify the path. Spring framework will magically manages it out of the box.

ASK
  • 1,136
  • 1
  • 10
  • 14