When using @Mock my @Test does not work although validating through a simple debug works completely, I do not understand how to use 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 a problem and is very basic, however the second, which is apparently just as trivial, returns me:
Expected: a string containing "SEPAROUTING_V3_FULL_" but: was null
As you can see I am making a mock of:
@Component
public class SepaRoutingUtils {
public String readSepaXMLFile() {
return "ARCHIVE_20141224.xml";
}
}
Which for me is basically the same, but I realize that I don't understand how @Mock works and I can't do an @Autowired of that class either because I think it shouldn't be done in the @Test.
I see that making 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 obtain a value with @Value:
@Value("${from.folder}")
private String FROM_FOLDER;
I know that I can be wrong and several concepts.
class updateSepaRoutingUtils
@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();
}
}
}
Complementing the generally correct answer of @SJuan76.
For your particular case, you want to make a mock of a private member which you cannot do
@Mock/@Spy
because it is a final class (String)A possible solution could be to use reflection to add a testable value to that String:
You do
mocks
to replace components that the code you test depends on. This way you can test a component without depending on whether the replaced components work correctly or not.Your component does something like this 1 :
How do you test
getSuma()
? The value you get will depend on what itpersistencia.getNumeros()
returns, so if you don't control that you won't be able to do the test (or at least, you won't be able to validate the result).So
And yes, you could just "handwrite" your implementation of
MiComponentePersistencia
to do the same thing. The frameworks ofmock
simply make the task much easier for you.1 I actually have more experience with Java EE and Mockito than I do with Spring, so you'll excuse me if something about Spring isn't quite right.