Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996656
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:18:21+00:00 2026-05-20T07:18:21+00:00

Im trying to mock a new File generated in a loop. Simplified example: class

  • 0

Im trying to mock a new File generated in a loop. Simplified example:

class FileClass {

  def basePath
  def listObjects = []

  def createFilePerObject() {
    listObjects.each {currentObject ->
      File currentFile = new File("${basePath.toString()}/${currentObject.objectName}")

      currentFile.write currentObject.objectContent   //Verify this behaviour!!
    }
  }

}

class SimpleObject {
  String objectName
  String objectContent
}

And, the test:

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 << object1
    listObjects << object2
    listObjects << object3
    listObjects << 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(_)
  }

}

So, as you can see, im trying to verify that new files actually have something written in them.

And, i get the following error:

MockFor with constructor interception enabled is only allowed for Groovy objects but found: java.io.File

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.

And, following the logic of the error, i decided to actually ovveride the File constructor like this:

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 << object1
    listObjects << object2
    listObjects << object3
    listObjects << object4

    fileClass = new FileClass(basePath: ".", listObjects: listObjects)

    def mockFile = new MockFor(File)

    File.metaClass.constructor << {String filePath -> mockFile } //Return the mocked file, so it can be verified

    when:
    mockFile.use {
      fileClass.createFilePerObject()
    }

    then:
    1 * mockFile.write(_)
  }

}

And recived the warning that the constructor already exists(i guess i cant ovveride it after all):

groovy.lang.GroovyRuntimeException: Cannot add new constructor for arguments [[class java.lang.String]]. It already exists!

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.

So, PowerMock doesnt work(cant load PowerMockRunner), any ideas how to mock this, not using additional libraries, and not end up with useless classes?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:18:21+00:00Added an answer on May 20, 2026 at 7:18 am

    Can I suggest alternative non pure testing approach.

    For this one test create a temporary directory, set basePath in your test subject and allow the test to actually write out the file. Your assertations will obviously have read the file in to verify the result, but it would be a complete test with out attempting to mock out JDK classes. The test teardown can then clean up the temp dir.

    If you really are set on pure in memory unit testing, I’m afraid you might have to use JMockIt which if you can get your head around it and the documentation can mock just about anything including parts of the JDK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jmock and trying to mock an HttpSession. I am getting:
I'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
I am new to mock objects, and I am trying to learn how to
I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code
I'm having a lot of trouble trying to define a mock for a rails
I'm trying to mock MouseButtonEventArgs.GetPosition() with Moq, but I keep receiving this error: System.ArgumentException:
I'm trying to mock the OpenID handling in my cucumber tests. For this purpose
I'm trying to mock an interface's events like the following: [TestMethod] public void NeedingDataFiresEvents()
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.