Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [Mockito testing] Problem Setting a mocked IWorkspaceRoot to return desired files(Setting a mocked IWorkspaceRoot to return desired java.io.File's)
[Mockito testing] Problem Setting a mocked IWorkspaceRoot to return desired files [message #1156365] Sat, 26 October 2013 13:56
Stefan Prisca is currently offline Stefan Prisca
Messages: 7
Registered: July 2013
Junior Member
Hi,

I am trying to write a unit test for the save-as action of an editor using Mockito.

I created a temporary folder, a Mock WorkspaceRoot, and there are two files with coresponding IPaths:
    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();
    
    IPath newFilePath;
    IPath originalFilePath;
    
    File newFile;
    File originalFile;
    
    @Mock
    private WorkspaceRoot root;
.

Afterwards I set the root location to the tempFolder, created the files and set the paths accordingly:
        File location = tempFolder.newFolder("someNewFolder");
        IPath path= new Path(location.getAbsolutePath());
        when(root.getLocation()).thenReturn(path);
        newFile=new File(location, NEW_FILE);
        originalFile=new File(location, ORIGINAL_FILE);
        createOriginalFile();
        newFilePath=new Path(newFile.getAbsolutePath()).makeRelativeTo(root.getLocation());
        originalFilePath=new Path(originalFile.getAbsolutePath()).makeRelativeTo(root.getLocation());


The plan is to call the
performSaveAsAction(IWorkspaceRoot root, IPath newPath, IPath originalPath)
with the mocked workspace root and the path for the files that I created, like this:
performSaveAsAction(root, newFilePath, originalFilePath)

This should create a new file using the newFilePath with the contents of the file found at originalFilePath.
But I cannot set the root to return either of the files, as the getFile() method needs to return an IFile and both files are java.io.File:

when(root.getFile(originalFilePath)).thenReturn(originalFile);
when(root.getFile(newFilePath)).thenReturn(newFile);


Any ideas on how to set the root to return the files I created, or how I could create IFiles in the desired directory, without setting up a whole project?

Thank you!
Previous Topic:Exporting Ant scripts
Next Topic:Re: Server
Goto Forum:
  


Current Time: Thu Oct 23 06:11:40 GMT 2014

Powered by FUDForum. Page generated in 0.02075 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software