Amit Kumar
Amit Kumar

Reputation: 2745

Junit 4.11 TemporaryFolder constructor with java.io.File

I am trying to do this. and it ids working fine in eclipse but gives error in maven.

Sample Code

File baseFolder=new File("C:\BASE_FOLDER");
temporaryFolder = new TemporaryFolder(baseFolder);


//Error in maven
[ERROR] symbol  : constructor TemporaryFolder(java.io.File)
[ERROR] location: class org.junit.rules.TemporaryFolder

Any links or hints will be helpful. Thanks.

Full Error:

symbol  : constructor TemporaryFolder(java.io.File)
location: class org.junit.rules.TemporaryFolder

        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure

Upvotes: 0

Views: 525

Answers (1)

matsev
matsev

Reputation: 33749

Since TemporaryFolder is a TestRule, you use it together with the @Rule annotation. From the Javadoc:

public static class HasTempFolder {
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void testUsingTempFolder() throws IOException {
        File createdFile = folder.newFile("myfile.txt");
        File createdFolder = folder.newFolder("subfolder");
        // ...
    }
}

or if you would like to specify the location:

File baseFolder = new File("C:\BASE_FOLDER");
@Rule
public TemporaryFolder folder = new TemporaryFolder(baseFolder);

Upvotes: 2

Related Questions