Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Writing content to TemporaryFolder and deleting file after the test

 
Ranch Hand
Posts: 99
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?
 
Sheriff
Posts: 13551
223
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
Sheriff
Posts: 13551
223
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: 99
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: 99
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!