Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking groovy new File.eachFile()
    text
    copied!<p>I'm having trouble mocking file.eachFile() {}.</p> <p>I'm trying to mock the files that get returned from file.eachFile{} - i don't want to actually test the contents of a real directory on my drive.</p> <p>I don't see how i can achive this using <strong>Spock</strong>(as i understand it's used more for mock verification).</p> <p>So, i decided to use the "native" <strong>Groovy</strong> <em>mock capabilities</em>, and the <strong>MOP</strong> capabilities. No luck.</p> <p>For example(i had to remove the File type in order to pass the object to the testing funcion):</p> <pre><code>def mockFile = new MockFor(java.io.File) mockFile.demand.eachFile {[new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each {it}} </code></pre> <p>Or(using metaclass):</p> <pre><code>File mockFile = new File("irrelevant_path") mockFile.metaClass.eachFile = {[new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each {it}} </code></pre> <p>I eachFile method is defined as:</p> <pre><code>public static void eachFile(final File self, final Closure closure) throws FileNotFoundException, IllegalArgumentException { eachFile(self, FileType.ANY, closure); } </code></pre> <p>I'm already a litty bit fuzzy from all the coding attempts to mock this :)</p> <p>I managed to reach the method(no runtime exceptions), but am unable to actually see the injection of the File list as defined beforehand.</p> <p>The tested method is(for simplicity):</p> <pre><code>folderSourceScripts.eachFile {File currentFile -&gt; ..... } </code></pre> <p>In Java, Mockito would do this in a minute, but i read somewhere that there were some issues regarding integration with Groovy. There is a problem when mocking the classes because Groovy makes a call to the Metaclass.</p> <p>I saw this <a href="http://groovy.329449.n5.nabble.com/Is-anyone-using-Mockito-to-Mock-Groovy-Classes-in-Groovy-Test-Cases-td370887.html" rel="nofollow">here</a>.</p> <p><strong>tim_yates</strong> wrote:</p> <pre><code>def mock = new MockFor(File) mock.demand.eachFile { [new File("script1.sql"), new File("script2.syn"), new File("script3.grt")].each { (it) } } mock.use { new File('.').eachFile {file -&gt; println "$file" } </code></pre> <p>The script works, but there's a catch when using it in the test.</p> <p>As i mentioned, i'm using Spock and the file variable is passed in a method. So using it the way you wrote it causes:</p> <pre><code>groovy.lang.MissingMethodException: No signature of method: groovy.mock.interceptor.MockFor.eachFile() is applicable for argument types: ... </code></pre> <p>I'm guessing thats because there isnt direct usage of File.eachFile() in the code(it doesn't pass the mock to the method, it expects the mock to be instantiated in the "use" block)?</p> <p>The code in the test looks like this: </p> <pre><code>when: mock.use { folderValidator.validate(mock) } then: folderValidator.listMissingFiles.size == 3 </code></pre> <p>The problem with proxyInstance(), which should pass the mock into the method is, as the example will show:</p> <pre><code>when: folderValidator.validate(mockProxy) then: folderValidator.listMissingFiles.size == 3 </code></pre> <p>When the mock is passed, groovy can't instantiate the class, because it doesn't know which constructor to use?</p> <pre><code>org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method &lt;init&gt;() to invoke from this list: private java.io.File#&lt;init&gt;(java.lang.String, int) private java.io.File#&lt;init&gt;(java.lang.String, java.io.File) public java.io.File#&lt;init&gt;(java.lang.String) public java.io.File#&lt;init&gt;(java.net.URI) public java.io.File#&lt;init&gt;(java.io.File, java.lang.String) public java.io.File#&lt;init&gt;(java.lang.String, java.lang.String) </code></pre> <p>In theory it does the work, but in this example it fails, maybe I'm coding it wrong. I started using Groovy two weeks ago, so im still getting used to it. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload