I am trying to implement TDD and code coverage in my new projects but for one that I am developing right now I have tried to create some test cases, but I have multiple doubts respect. Here I share the class that I created to copy a file from my file system:
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.google.common.io.Files;
@Component
public class CopyXMLFileToResources {
@Value("${xml.routing.folder}")
private String XML_FOLDER;
@Value("${resources.folder}")
private String RESOURCES_FOLDER;
private final Logger LOG = LoggerFactory.getLogger(CopyXMLFileToResources.class);
@Scheduled(cron = "${cron.files.copy}", zone = "${zone.files.copy}")
public void copyXMLFile() throws IOException {
LOG.info("Copying the lastest varsion of Delta file ...");
File directory = validateDirectory(XML_FOLDER);
if (directory.canRead()) {
try {
File origin = Utils.prepareDeltaFile(directory);
if (origin != null) {
File copied = new File(RESOURCES_FOLDER + origin.getName());
if (copied.getTotalSpace() > 0) {
LOG.info("Last version already copied.");
}
else {
try {
Files.copy(origin, copied);
LOG.info(
"Copying the DELTA file successful");
}
catch (Exception e) {
LOG.error(e.getMessage());
}
}
}
else {
LOG.warn("No matching files fouded in {}",
directory.getCanonicalPath());
}
}
catch (ParseException e) {
LOG.error(e.getMessage());
}
}
else {
LOG.warn("Cannot read folder {}. ", XML_FOLDER);
}
}
private File validateDirectory (String folder) {
return new File(folder);
}
public String printString(String s ) {
return s;
}
}
I know there are many things to improve, but my first question is, what features should be tested and where to start? Poraue I created a class Test but I do not know what to try, I tried to start by trying the access to the folder but this made me think if I need to return something as a code depending on the case, for example 0 for access denied, 1 when everything goes well for example. This class has only one public method and is of type void . I also have questions about the properties I am using:
application.properties
:
xml.routing.folder=../../xml
resources.folder=src/main/resources/
cron.files.copy=0 1 1 * * *
zone.files.copy=Europe/Paris
Is it correct to do it this way? If I change the project to another server, access to the folder containing the xml can be denied. I have not created Test before so I would appreciate recommendations to get started, especially from TDD.