When using @Mock my @Test does not work although validating through a simple debug works completely, I do not understand how to use the Mocks and what I am testing is very simple:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
public class SepaRoutingFromXMLTest {
@Mock
SepaRoutingUtils sepa;
@Test
public void existValidOneFullXMLFileInFolder() throws Exception {
assertThat("ARCHIVE_20141224.xml", containsString(".xml"));
assertThat(sepa.readSepaXMLFile(), containsString("ARCHIVE_"));
}
}
The first assertThat
works without problem is very basic, however the second one that apparently is just as trivial returns me:
Expected: a string containing "SEPAROUTING_V3_FULL_" but: was null
As you can see I'm doing a mock of:
@Component
public class SepaRoutingUtils {
public String readSepaXMLFile() {
return "ARCHIVE_20141224.xml";
}
}
That for me is basically the same, but I realize that I do not understand the functioning of @Mock and I can not do an @Autowired of that class because I think that should not be done in @Test.
I see that when doing the following modification works correctly:
@RunWith(SpringRunner.class)
public class SepaRoutingFromXMLTest {
@Test
public void existValidOneFullXMLFileInFolder() throws Exception {
SepaRoutingUtils sepa = new SepaRoutingUtils();
assertThat(sepa.readSepaXMLFile(), containsString(".xml"));
assertThat(sepa.readSepaXMLFile(), containsString("ARCHIVE_"));
}
}
However, doing this does not help me because in the class SepaRoutingUtils
I must get a value with @Value:
@Value("${from.folder}")
private String FROM_FOLDER;
I know I can be wrong with several concepts.
Updating class SepaRoutingUtils
@Component
public class SepaRoutingUtils {
@Value("${from.folder}")
private String FROM_FOLDER;
private File readSepaFolder() {
File folder = new File(FROM_FOLDER);
if(!folder.isDirectory()) {
throw new FolderAccessDeniedException();
}
return folder;
}
public String readSepaXMLFile(SepaRoutingFileType fileType) {
try {
return Utils.prepareXMLFile(readSepaFolder(), fileType).getName();
}
catch (ParseException e) {
e.printStackTrace();
throw new NotValidFileException();
}
}
}