Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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?
 
Sheriff
Posts: 15506
263
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: 15506
263
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic