how to get mutation coverage for void method that checking file existence and doing some math operation to delete file

837 views
Skip to first unread message

Arya

unread,
Jun 23, 2016, 11:55:34 AM6/23/16
to PIT Users
hi

how to get mutation coverage for void method that checking file existence and doing some math operation to delete file.


Mutation_issue.png

Martin Schröder

unread,
Jun 23, 2016, 12:44:43 PM6/23/16
to pitu...@googlegroups.com
2016-06-23 17:55 GMT+02:00 Arya <arya...@gmail.com>:
> how to get mutation coverage for void method that checking file existence
> and doing some math operation to delete file.

By writing unit tests. I'd refactor the code, though, to make it testable.

http://stackoverflow.com/a/16045378/821436

Best
Martin

Stefan

unread,
Jun 23, 2016, 4:03:50 PM6/23/16
to pitu...@googlegroups.com, arya...@gmail.com
Hi Arya,

you can get mutation coverage the same way you would get regular code coverage/condition coverage in that case:

- Make sure you've a test case where "directory" exists and one where it doesn't. Check that a file gets deleted.
- Create a file with an appropriate "lastModified" timestamp that will be lower than threshold and check that the file gets deleted
- create a file with an appropriate "lastModified" timestamp that will be larger than the threshold  and check that the file was not deleted
- create a file with an appropriate "lastModified" timestamp that exactly matches your threshold and check that the file gets deleted
- create test cases with multiple files
- create a test case where your process has not the correct rights to delete the file thus, file.delete() should return false --> check that a message "Unable to delete file" gets logged


I think it will be hard to create a test case where lastModified will exactly match your threshold. You'll need to provide system time as a parameter or mock-it-out and fix the time for your test case.
You need to capture the system out to get the log messages or you need to mock out the LOG class used.
I don't know whether there is a way to manipulate lastModified time. You'll need to mock that logic out if there is no way to manipulate the timestamp. For example

interface FileKeeper {
  boolean mayDelete(File file);
}

Splitting you logic over specialized classes makes testing easier.


Regards
Stefan



Am 23.06.2016 um 17:55 schrieb Arya:
hi

how to get mutation coverage for void method that checking file existence and doing some math operation to delete file.


--
You received this message because you are subscribed to the Google Groups "PIT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pitusers+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages