I use the SpringBoot mail component to generate eml files. When I use mockito to simulate, Why would a real file be generated?
This is my demo
#Resource
private JavaMailSender javaMailSender;
public File createEml(String filename) throws IOException, MessagingException {
File file = new File(filename);
try(FileOutputStream fos = new FileOutputStream(file)) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
mimeMessage.writeTo(fos);
}
return file;
}
}
test
#InjectMocks
private EmailManager emailManager = new EmailManager();
#Mock
private JavaMailSender javaMailSender;
#BeforeEach
private void setUp() {
MockitoAnnotations.openMocks(this);
}
#Test
void testCreateEml() throws MessagingException, IOException {
MimeMessage mimeMessage = Mockito.mock(MimeMessage.class);
Mockito.doNothing().when(mimeMessage).writeTo(Mockito.any(FileOutputStream.class));
Mockito.when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
emailManager.createEml("test.eml");
}
Because you don’t create a mock of EmailManager
Usually #InjectMocks annotation is use on the class that you’re about to test (EmailManager in this case)
This is a real code that you don’t want to mock (there is no point to write a test that tests a mock) - you always want to test the real code and mocks is just an auxiliary tool to “mimic” the behavior of dependencies
So in the EmailManager the method createEml is real - and file creation is just what it does - you’ve posted the source code of it. This code gets executed during the test. That’s it.
So if you want to create a test that doesn’t create any real file you should refactor the file creation part in a way that it will support file creation of “virtual” files in memory. After all not any arbitrarily-written pieces of code can are automatically unit-testable, you have to design the code with “unit-test ability” in mind
Related
I am trying to write a JUnit test for a java code that with three methods.
FileDeletion.java:
public static void fileDeletion(String filePath) {
File file = new File(filePath);
file.delete();
}
I looked online on how to test this, but it came up with how to make a temporary file in JUnit which is "guaranteed to be deleted after the test finishes".
See: https://howtodoinjava.com/junit/junit-creating-temporary-filefolder-using-temporaryfolder-rule/
How do I make a file which can be created and then subsequently deleted in a JUnit Test?
Any help would go a long way, many thanks.
I would consider using JUnit's TemporaryFolder rule to help you set up the file and directory structure you need for your test which is cleaned up after your test.
public class DeleteFileTest {
#Rule
public TemporaryFolder folder = new TemporaryFolder();
#Test
public void test() {
final File file = folder.newFile("myfile1.txt");
file.delete();
//Assert
}
}
public class MyUtil {
public static Properties loadProperties() throws Exception {
Properties prop = new Properties();
InputStream inputStream = MyUtil.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
if (inputStream != null) {
prop.load(inputStream);
}
return prop;
}
}
I have written test case for the above method, when I ran as test case in eclipse it's passing and when I debug loadProperties() is not getting called and cobertura report is showing as uncovered code.
#RunWith(PowerMockRunner.class)
#PrepareForTest({ MyUtil.class, Properties.class })
#Test
public void testLoadProperties() throws Exception{
String fileName = "application.properties";
Properties mockProps = PowerMockito.mock(Properties.class);
PowerMockito.mockStatic(Properties.class);
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(mockProps);
InputStream mockInputStream = Mockito.mock(InputStream.class);
PowerMockito.mockStatic(MyUtil.class);
ClassLoader mockClassLoader = Mockito.mock(ClassLoader.class);
PowerMockito.when(MyUtil.class.getClassLoader()).thenReturn(mockClassLoader);
PowerMockito.when(mockClassLoader.getResourceAsStream(fileName)).thenReturn(mockInputStream);
PowerMockito.doNothing().when(mockProps).load((InputStream)Mockito.any());
MyUtil.loadProperties();
//assertNotNull("Not Null", MyUtil.loadProperties()); //assert failing
}
what should I change to make sure that my code actually covers in code coverage?
It's old well-known issue that PowerMock breaks code coverage tools:
https://github.com/cobertura/cobertura/issues/94
In current moment, there is only one way to get code coverage JaCoCo Offline instrumenting
https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo
I'm writing a UDF for Pig using Java. It works fine but Pig doesn't give me options to separate environment. What my Pig script is doing is to get Geo location from IP address.
Here's my code on the Geo location part.
private static final String GEO_DB = "GeoLite2-City.mmdb";
private static final String GEO_FILE = "/geo/" + GEO_DB;
public Map<String, Object> geoData(String ipStr) {
Map<String, Object> geoMap = new HashMap<String, Object>();
DatabaseReader reader = new DatabaseReader.Builder(new File(GEO_DB)).build();
// other stuff
}
GeoLite2-City.mmdb exists in HDFS that's why I can refer from absolute path using /geo/GeoLite2-City.mmdb.
However, I can't do that from my JUnit test or I have to create /geo/GeoLite2-City.mmdb on my local machine and Jenkins which is not ideal. I'm trying to figure out a way to make my test passed while using new File(GEO_DB) and not
getClass().getResourceAsStream('./geo/GeoLite2-City.mmdb') because
getClass().getResourceAsStream('./geo/GeoLite2-City.mmdb')
Doesn't work in Hadoop.
And if I run Junit test it would fail because I don't have /geo/GeoLite2-City.mmdb on my local machine.
Is there anyway I can overcome this? I just want my tests to pass without changing the code to be using getClass().getResourceAsStream and I can't if/else around that because Pig doesn't give me a way to pass in parameter or maybe I'm missing something.
And this is my JUnit test
#Test
#Ignore
public void shouldGetGeoData() throws Exception {
String ipTest = "128.101.101.101";
Map<String, Object> geoJson = new LogLine2Json().geoData(ipTest);
assertThat(geoJson.get("lLa").toString(), is(equalTo("44.9759")));
assertThat(geoJson.get("lLo").toString(), is(equalTo("-93.2166")));
}
which it works if I read the database file from resource folder. That's why I have #Ignore
Besides, your whole code looks pretty un-testable.
Every time when you directly call new in your production code, you prevent dependency injection; and thereby you make it much harder to test your code.
The point is to not call new File() within your production code.
Instead, you could use a factory that gives you a "ready to use" DatabaseReader object. Then you can test your factory to do the right thing; and you can mock that factory when testing this code (to return a mocked database reader).
So, that one file instance is just the top of your "testing problems" here.
Honestly: don't write production code first. Do TDD: write test cases first; and you will quickly learn that such production code that you are presenting here is really hard to test. And when you apply TDD, you start from "test perspective", and you will create production code that is really testable.
You have to make the file location configurable. E.g. inject it via constructor. E.g. you could create a non-default constructor for testing only.
public class LogLine2Json {
private static final String DEFAULT_GEO_DB = "GeoLite2-City.mmdb";
private static final String DEFAULT_GEO_FILE = "/geo/" + GEO_DB;
private final String geoFile;
public LogLine2Json() {
this(DEFAULT_GEO_FILE);
}
LogLine2Json(String geoFile) {
this.geoFile = geoFile;
}
public Map<String, Object> geoData(String ipStr) {
Map<String, Object> geoMap = new HashMap<String, Object>();
File file = new File(geoFile);
DatabaseReader reader = new DatabaseReader.Builder(file).build();
// other stuff
}
}
Now you can create a file from the resource and use this file in your test.
public class LogLine2JsonTest {
#Rule
public final TemporaryFolder folder = new TemporaryFolder();
#Test
public void shouldGetGeoData() throws Exception {
File dbFile = copyResourceToFile("/geo/GeoLite2-City.mmdb");
String ipTest = "128.101.101.101";
LogLine2Json logLine2Json = new LogLine2Json(dbFile.getAbsolutePath())
Map<String, Object> geoJson = logLine2Json.geoData(ipTest);
assertThat(geoJson.get("lLa").toString(), is(equalTo("44.9759")));
assertThat(geoJson.get("lLo").toString(), is(equalTo("-93.2166")));
}
private File copyResourceToFile(String name) throws IOException {
InputStream resource = getClass().getResourceAsStream(name);
File file = folder.newFile();
Files.copy(resource, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file;
}
}
TemporaryFolder is a JUnit rule that deletes every file that is created during test afterwards.
You may modify the asserts by using the hasToString matcher. This will give you more detailed information in case of a failing test. (And you have to read/write less code.)
assertThat(geoJson.get("lLa"), hasToString("44.9759"));
assertThat(geoJson.get("lLo"), hasToString("-93.2166"));
You don't. Your question embodies a contradiction in terms. Resources are not files and do not live in the file system. You can either distribute the file separately from the JAR and use it as a File or include it in the JAR and use it as a resource. Not both. You have to make up your mind.
I am trying to test a method that copies a source file to a dest file using JUnit's TemporaryFolder. I get a Java IOException when I try run this test however. Does it matter where I make the declaration for the folder? (My test class has several different tests in it). And if so, what is the proper way to do it? I ask because I currently have several unit tests above this code, then I try to set up the testing for the file copying. Maybe the #Rule-#Before-#Test block needs to be in its own class? Here is the snippet where I have coded the test:
...other tests...then:
#Rule
public static TemporaryFolder tmp = new TemporaryFolder();
private File f1, f2;
#Before
public void createTestData() throws IOException {
f1 = tmp.newFile("src.txt");
f2 = tmp.newFile("dest.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f1));
out.write("This should generate some \n" +
"test data that will be used in \n" +
"the following method.");
out.close();
}
#Test
public void copyFileTest() {
out.println("file 1 length: " + f1.length());
try {
copyFile(f1, f2);
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
}
if (f1.length() != f2.length())
fail();
else if (!f1.equals(f2))
fail();
assertSame(f1, f2);
}
When I run this test class, all 11 of my tests now fail (which previously passed) and I get java.io.IOException: No such file or directory.
So looking at the JUnit Javadoc, I have found out that any declaration under #Rule must be public, and not static. So I took out the static and just have:
#Rule
public TemporaryFolder tmp = new TemporaryFolder();
I still do not know for sure if it matters where this declaration is made when you have other unit tests in your class that do not use the #Rule declaration, but this did allow me to run through my tests successfully.
If you really want to declare TemporaryFolder as static, you can use #ClassRule which is used to annotate static fields that contains Rule.
#ClassRule
public static TemporaryFolder tmp = new TemporaryFolder();
Reference: http://junit-team.github.io/junit/javadoc/4.10/org/junit/ClassRule.html
I'm creating a TemporaryFolder using the #Rule annotation in JUnit 4.7. I've tried to create a new folder that is a child of the temp folder using tempFolder.newFolder("someFolder") in the #Before (setup) method of my test. It seems as though the temporary folder gets initialized after the setup method runs, meaning I can't use the temporary folder in the setup method. Is this correct (and predictable) behavior?
This is a problem in Junit 4.7. If you upgrade a newer Junit (for example 4.8.1) all #Rule will have been run when you enter the #Before method:s. A related bug report is this: https://github.com/junit-team/junit4/issues/79
This works as well. EDIT if in the #Before method it looks like myfolder.create() needs called. And this is probably bad practice since the javadoc says not to call TemporaryFolder.create(). 2nd Edit Looks like you have to call the method to create the temp directories if you don't want them in the #Test methods. Also make sure you close any files you open in the temp directory or they won't be automatically deleted.
<imports excluded>
public class MyTest {
#Rule
public TemporaryFolder myfolder = new TemporaryFolder();
private File otherFolder;
private File normalFolder;
private File file;
public void createDirs() throws Exception {
File tempFolder = myfolder.newFolder("folder");
File normalFolder = new File(tempFolder, "normal");
normalFolder.mkdir();
File file = new File(normalFolder, "file.txt");
PrintWriter out = new PrintWriter(file);
out.println("hello world");
out.flush();
out.close();
}
#Test
public void testSomething() {
createDirs();
....
}
}