如何使用Java代码刷新文件夹?

如何使用Java代码刷新文件夹?,java,maven,testing,automated-tests,cucumber,Java,Maven,Testing,Automated Tests,Cucumber,我想做什么: 在测试执行完成后生成测试报告,然后通过电子邮件发送(我使用的是Maven、cucumber和Restasured) 我面临的问题: 上面提到的所有事情都在发生,除了通过电子邮件发送的测试报告实际上来自之前的测试执行。也就是说,在运行通过电子邮件发送报告的代码时,报告或包含该报告的文件夹不会被刷新 是否有任何方法可以在测试执行结束时通过maven刷新文件夹,或者是否有任何其他方法可以发送最新的测试报告 任何指针都是非常感谢的。下面是我的Cumber runner代码 @RunWith

我想做什么:

在测试执行完成后生成测试报告,然后通过电子邮件发送(我使用的是Maven、cucumber和Restasured)

我面临的问题:

上面提到的所有事情都在发生,除了通过电子邮件发送的测试报告实际上来自之前的测试执行。也就是说,在运行通过电子邮件发送报告的代码时,报告或包含该报告的文件夹不会被刷新

是否有任何方法可以在测试执行结束时通过maven刷新文件夹,或者是否有任何其他方法可以发送最新的测试报告

任何指针都是非常感谢的。下面是我的Cumber runner代码

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/Feature"
    ,glue= "com/sss/tests/RestApi"
    ,tags = {"@TestInDevelopment"}
    ,plugin = {"com.cucumber.listener.ExtentCucumberFormatter:/Report.html"}        
    ,format = {"pretty","html:target/cucumber"}
    ,monochrome = true
    )

public class TestRunner { 
static Configurations config = new Configurations();

@BeforeClass
public static void setup(){

    /*
     * Loading the log4j configurations, before the start of test execution
     */     
    String folderName = "PropertyFiles";
    String fileName = "log4j.properties";
    PropertyConfigurator.configure(config.getResourcePath(folderName)+fileName);

}

@AfterClass
public static void teardown() throws UnknownHostException, IOException, InterruptedException {

    /*
     * Loading the configuration file for the test execution report and setting up the user name, OS info and host details in the report
     */     
    Reporter.loadXMLConfig(new File(config.getResourcePath("Resources")+"extent-config.xml"));
    Reporter.setSystemInfo("USER", System.getProperty("user.name"));
    Reporter.setSystemInfo("OS", System.getProperty("os.name"));
    Reporter.setSystemInfo("HOST NAME", InetAddress.getLocalHost().getHostName());  

   /*
    * After the test execution is completed, parsing the generated test execution report to to see if there is any failures and based on that,
    * sending the Build Success/Failure e-mail to the configured mail list
    */        
    HTML_TestExecutionReportParser testExecutionStatusParser = new HTML_TestExecutionReportParser();    
    TestExecutionReportEmailSender testExecutionReportMailSender = new TestExecutionReportEmailSender();        
    testExecutionReportMailSender.sendTestExecutionReportEmail(testExecutionStatusParser.isAssertionFailureFound(), testExecutionStatusParser.errorsFoundList());     

}

}

您可以尝试将电子邮件代码放在
JVM关闭挂钩中。因此,在完成其他每个进程时将调用它


关闭挂钩的示例--

执行
@AfterClass
时,您确定报告已写入吗?我删除了旧报告并运行了测试,它引发了FileNotFoundException。我假设报告是在执行
@AfterClass
期间编写的,并且仍然需要在文件夹级别进行刷新。但是如果报告是在执行
@AfterClass
之后编写的,我只是想知道我将在哪里处理电子邮件部分,因为
@AfterClass
应该是最后执行的代码块。请尝试将电子邮件代码放在jvm关闭挂钩中。也许它有用。谢谢@Grasshopper,将电子邮件代码放在关机挂钩中对我很有效,它现在正在发送最新的报告,而不是旧的报告。虽然我不确定这是一个好主意还是一个好做法,但目前它确实满足了我的需要。如果您能将此作为您的答案发布,我可以向上投票。@卡扎菲唯一的解决方案是查看格式化程序的源代码,以确定何时调用flush()方法。由于我的声誉不到15,我也不允许向上投票。对不起,不用担心。。。没什么可遗憾的