Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Writing content to TemporaryFolder and deleting file after the test

 
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to write some strings to File created by TemporaryFolder. I need to provide 3 Strings:

Line1 a
Line2 b c
Line 3

1) I make this, but it does not add anything. Length of testedContent is still 0. I tried to flush FileWriter but nothing happened. What am I making wrong?



Stacktrace:



2) Should I do it in setup with @Before method or do it inside test?

3) I read that this folder will be deleted automatically, so I dont need to perform .deleteOnExit() on textFile?
 
Marshal
Posts: 15631
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried this:
 
Junilu Lacar
Marshal
Posts: 15631
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the same token, I would have expected you to be able to do this:

I would have expected the getContentFile() method to be overloaded so that it can take a String path argument as well as a File file argument.

Also, that method name is somewhat misleading. If it's getting the content of the file specified by the argument, a better name for that method would be just getContent(). That way, the code wouldn't be so repetitive and stutter-y.
 
must Janik
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Have you tried this:



Ye, I have tried.
My method looks like this:


and I have java.lang.AssertionError: array lengths differed, expected.length=3 actual.length=1.

Is it necessary to overload the getContent method? Because I will use method with textFile as a parameter only in this unit test. I will paste the method I want to unit-test, too:




 
must Janik
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Forgot about `\n`, that's why everything was in one line.
Then, could you tell me if I should call deleteOnExit() on File?
I read, that TemporaryFolder deletes everything alone.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic