Note that there are some explanatory texts on larger screens.

plurals
  1. POGroovy mocking File constructor, verify write on newly created files
    text
    copied!<p>Im trying to mock a new File generated in a loop. Simplified example:</p> <pre><code>class FileClass { def basePath def listObjects = [] def createFilePerObject() { listObjects.each {currentObject -&gt; File currentFile = new File("${basePath.toString()}/${currentObject.objectName}") currentFile.write currentObject.objectContent //Verify this behaviour!! } } } class SimpleObject { String objectName String objectContent } </code></pre> <p>And, the test:</p> <pre><code>class FileClassTest extends Specification { FileClass fileClass def "test simple object"() { def listObjects = [] SimpleObject object1 = new SimpleObject(objectName: "First object", objectContent: "First object content") SimpleObject object2 = new SimpleObject(objectName: "Second object", objectContent: "Second object content") SimpleObject object3 = new SimpleObject(objectName: "Third object", objectContent: "Third object content") SimpleObject object4 = new SimpleObject(objectName: "Fourth object", objectContent: "Fourth object content") listObjects &lt;&lt; object1 listObjects &lt;&lt; object2 listObjects &lt;&lt; object3 listObjects &lt;&lt; object4 fileClass = new FileClass(listObjects: listObjects, basePath: ".") def dummyFile = new MockFor(File) def mockFile = new MockFor(File, true) //Intercept constructor call mockFile.demand.with { File() {dummyFile} } when: mockFile.use { fileClass.createFilePerObject() } then: 1 * mockFile.write(_) } } </code></pre> <p>So, as you can see, im trying to verify that new files actually have something written in them.</p> <p>And, i get the following error:</p> <pre><code>MockFor with constructor interception enabled is only allowed for Groovy objects but found: java.io.File </code></pre> <p>So, File is, as i understand, extended in groovy GDK(Groovy JDK), and it has additional(and very helpful) methods. But Groovy tries to mock java.io.File.</p> <p>And, following the logic of the error, i decided to actually ovveride the File constructor like this:</p> <pre><code>class FileClassTest extends Specification { FileClass fileClass def "test simple object"() { def listObjects = [] SimpleObject object1 = new SimpleObject(objectName: "First object", objectContent: "First object content") SimpleObject object2 = new SimpleObject(objectName: "Second object", objectContent: "Second object content") SimpleObject object3 = new SimpleObject(objectName: "Third object", objectContent: "Third object content") SimpleObject object4 = new SimpleObject(objectName: "Fourth object", objectContent: "Fourth object content") listObjects &lt;&lt; object1 listObjects &lt;&lt; object2 listObjects &lt;&lt; object3 listObjects &lt;&lt; object4 fileClass = new FileClass(basePath: ".", listObjects: listObjects) def mockFile = new MockFor(File) File.metaClass.constructor &lt;&lt; {String filePath -&gt; mockFile } //Return the mocked file, so it can be verified when: mockFile.use { fileClass.createFilePerObject() } then: 1 * mockFile.write(_) } } </code></pre> <p>And recived the warning that the constructor already exists(i guess i cant ovveride it after all):</p> <pre><code>groovy.lang.GroovyRuntimeException: Cannot add new constructor for arguments [[class java.lang.String]]. It already exists! </code></pre> <p>So, one of the ideas is to actually mock the File Factory that would produce the new File objects in the loop. However, the factory is, to say the least, useless - it would be used only to have the ability to test the file creation.</p> <p>So, PowerMock doesnt work(cant load PowerMockRunner), any ideas how to mock this, not using additional libraries, and not end up with useless classes?</p> <p>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