As others mentioned on Reddit, your code is not testable (yet). You cannot make the whole codebase testable in one go, you have to go incrementally. I've looked at the code and InfoService is a good starting point. You're injecting a InfoDto, which is really not doing much for you, just getting a reference with no fields set, which is as good as InfoDto infoDto = new InfoDto();
so you don't need injection for that, just use the constructor in the get()
method.
Next, your methods are all returning void. Some are really trivial to refactor, e.g. getUptimeInfo. You could make this return Map<String, String>
and set it in the get()
method, like this:
public InfoDto get() {
InfoDto infoDto = new InfoDto();
infoDto.setUptime(getUptimeInfo());
return infoDto;
}
The rest of the methods are not so simple to refactor and require some thoughts. If we look at getProcessorInfo
you're setting quite a bit of fields, but they really shouldn't be added directly to the infoDto as fields. Maybe create a ProcessorDto to hold those properties(cpu name, core count, clock speed, etc.) and InfoDto could use a single instance of ProcessorDto. You'd have to either change the implementation of the InfoDto getters to delegate to ProcessorDto, e.g. getCoreCount() { return processorDto.getCoreCount(); }
or remove the getters from InfoDto, move them to ProcessorDto and change the template to from info.coreCount
to info.processor.coreCount
which is probably much cleaner.
It should be easier to construct a InfoDto instance and have control of what it can hold, which in the end make it available to be the subject under test.
I realize this might look like quite a bit of work but it's actually rather minor when you start coding. If you would like me to continue adding some more tickets for things you can improve in your code, I'm happy to do so and maybe add a PR or two in the future, if time allows.