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

  • SEARCH
  • Home
  • 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 7007515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:37:02+00:00 2026-05-27T21:37:02+00:00

I wrote unit test (JUnit 4) that performs some logic and writes result to

  • 0

I wrote unit test (JUnit 4) that performs some logic and writes result to file. In @Before annotated method it creates file and in @After the file should be deleted. It isn’t though, and I can’t figure it out, why.

I am using Google Guava 10.01 Files API. Here’s my unit test code:

public class CashierTest extends ContextedTest {

    private File cashierFile;

    @Before
    public void createFile() throws Exception {
        cashierFile = new File("D://workspace-sts/spring-miso/cashier.txt");
        cashierFile.createNewFile();
    }

    @After
    public void release() {
        if (cashierFile.exists()) {
            if (!cashierFile.delete()) {
                System.out.println("Couldn't delete cashier file");
            }
        }
        cashierFile = null;
    }

    @Test
    public void testCashier() throws Exception {
        // file shouldn't contain any text
        assertFalse(Files.toString(cashierFile, Charset.defaultCharset()).length() > 0);

        Cashier cashier = (Cashier) context.getBean("cashier");
        ShoppingCart cart = (ShoppingCart) context.getBean("shoppingCartPrototype");
        cashier.checkout(cart);

        assertTrue(cashierFile.exists());
        // file should contain text now
        assertTrue(Files.toString(cashierFile, Charset.defaultCharset()).length() > 0);
    }

    @Override
    protected void setPath() {
        path = "sk/xorty/advancedioc/beans.xml";
    }
}

Note: ContextedTest superclass is my test which holds Spring container it isn’t relevant atm.

  • 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-27T21:37:03+00:00Added an answer on May 27, 2026 at 9:37 pm

    Simply instanting a File does not mean that an actual file will be created. Call createNewFile() or createTempFile() on that instance for this.

    Within your test method you don’t seem to pass that file reference to anyone that could possibly create the file or write anything in it… Am I missing something or is the code you posted missing some key lines ?

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

Sidebar

Related Questions

I have a method calling localtime that I wrote a unit test for. I
I need to write a unit test for a method that will print a
How do I write a unit test for a method that use an external
I want to run unit tests (Junit) on some model classes that typically will
What is the best way to unit test a method that calls into multiple
I have a stand-alone application that I wrote before I became test-infected. Its a
I'm using IntelliJ-Idea to write Java daemons. We use JUnit to unit test our
I am wondering should I write unit test for everything. There are some classes
How does one write a unit test that fails only if a function doesn't
I want to write a unit test for a Django manage.py command that does

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.