TemporaryFolder not usable inside setUp() method

View: New views
4 Messages — Rating Filter:   Alert me  

TemporaryFolder not usable inside setUp() method

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am using JUnit 4.7 and used the new Rules mechanism, with TempFolder. What I needed was to have a temporary folder where the CUT could create some folders, that I can then check and assert on them.

The problem that I encountered is that TempFolder is initialized only AFTER my setup method, therefore making it unusable in the set up of the test.

Here is my code:

-----------------------------------------------------
public class MyClassTest {
  @Rule
  public TemporaryFolder testFolder = new TemporaryFolder();

  @Before
  public void setUp() {
    // here testFolder has not been created
    // testFolder.getRoot() is null
  }

  @Test
  public void testSomething() {
    // here tempFolder is initialized
    // testFolder.getRoot() is not-null, and points to a temp folder
  }
}
-----------------------------------------------------

Is this ok and intended? For me would be much more logical to have the TemporaryFolder prepared when the setUp() method is invoked, so that I can then set up my mockups and my objects with this test folder. Otherwise, I have to place some set up code at the beginning of each test method, which basically invalidates the reason for text fixtures.

What do you think?

Regards,
Bogdan Mocanu


RE: TemporaryFolder not usable inside setUp() method

by kentb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bogdan,
 
You are seeing the currently expected behavior. It's clear that we need to
at least experiment with moving @Rules before @Before methods.
 
Kent

  _____  

From: junit@... [mailto:junit@...] On Behalf Of
Bogdan
Sent: Friday, September 18, 2009 6:48 AM
To: junit@...
Subject: [junit] TemporaryFolder not usable inside setUp() method


 

Hello,

I am using JUnit 4.7 and used the new Rules mechanism, with TempFolder. What
I needed was to have a temporary folder where the CUT could create some
folders, that I can then check and assert on them.

The problem that I encountered is that TempFolder is initialized only AFTER
my setup method, therefore making it unusable in the set up of the test.

Here is my code:

-----------------------------------------------------
public class MyClassTest {
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

@Before
public void setUp() {
// here testFolder has not been created
// testFolder.getRoot() is null
}

@Test
public void testSomething() {
// here tempFolder is initialized
// testFolder.getRoot() is not-null, and points to a temp folder
}
}
-----------------------------------------------------

Is this ok and intended? For me would be much more logical to have the
TemporaryFolder prepared when the setUp() method is invoked, so that I can
then set up my mockups and my objects with this test folder. Otherwise, I
have to place some set up code at the beginning of each test method, which
basically invalidates the reason for text fixtures.

What do you think?

Regards,
Bogdan Mocanu






[Non-text portions of this message have been removed]


Re: TemporaryFolder not usable inside setUp() method

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Kent,

thanks for answering. Only after sending the question, I saw that another user had a similar problem with TestName. Next time I will search the forums more thoroughly.

Regarding the rules, perhaps a good idea would be to create some event methods in the rules, to allow them to act on before @BeforeClass, after @BeforeClass, before @Before, after @Before, ... etc.

Not sure if this fits with the whole concept of Rules.

Regards,
Bogdan

--- In junit@..., "kentb" <kentb@...> wrote:

>
> Bogdan,
>  
> You are seeing the currently expected behavior. It's clear that we need to
> at least experiment with moving @Rules before @Before methods.
>  
> Kent
>
>   _____  
>
> From: junit@... [mailto:junit@...] On Behalf Of
> Bogdan
> Sent: Friday, September 18, 2009 6:48 AM
> To: junit@...
> Subject: [junit] TemporaryFolder not usable inside setUp() method
>
>
>  
>
> Hello,
>
> I am using JUnit 4.7 and used the new Rules mechanism, with TempFolder. What
> I needed was to have a temporary folder where the CUT could create some
> folders, that I can then check and assert on them.
>
> The problem that I encountered is that TempFolder is initialized only AFTER
> my setup method, therefore making it unusable in the set up of the test.
>
> Here is my code:
>
> -----------------------------------------------------
> public class MyClassTest {
> @Rule
> public TemporaryFolder testFolder = new TemporaryFolder();
>
> @Before
> public void setUp() {
> // here testFolder has not been created
> // testFolder.getRoot() is null
> }
>
> @Test
> public void testSomething() {
> // here tempFolder is initialized
> // testFolder.getRoot() is not-null, and points to a temp folder
> }
> }
> -----------------------------------------------------
>
> Is this ok and intended? For me would be much more logical to have the
> TemporaryFolder prepared when the setUp() method is invoked, so that I can
> then set up my mockups and my objects with this test folder. Otherwise, I
> have to place some set up code at the beginning of each test method, which
> basically invalidates the reason for text fixtures.
>
> What do you think?
>
> Regards,
> Bogdan Mocanu
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>



RE: Re: TemporaryFolder not usable inside setUp() method

by kentb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All,
 
I have changed the code on GitHub to run rules before running @Befores and
after running @Afters. It required one test change and moving one line of
implementation, so I'm fairly confident it won't cause problems. We'd
appreciate some feedback, though.
 
Kent

  _____  

From: junit@... [mailto:junit@...] On Behalf Of
Bogdan
Sent: Saturday, September 19, 2009 12:11 PM
To: junit@...
Subject: [junit] Re: TemporaryFolder not usable inside setUp() method


 

Hello Kent,

thanks for answering. Only after sending the question, I saw that another
user had a similar problem with TestName. Next time I will search the forums
more thoroughly.

Regarding the rules, perhaps a good idea would be to create some event
methods in the rules, to allow them to act on before @BeforeClass, after
@BeforeClass, before @Before, after @Before, ... etc.

Not sure if this fits with the whole concept of Rules.

Regards,
Bogdan

--- In junit@yahoogroups. <mailto:junit%40yahoogroups.com> com, "kentb"
<kentb@...> wrote:

>
> Bogdan,
>
> You are seeing the currently expected behavior. It's clear that we need to
> at least experiment with moving @Rules before @Before methods.
>
> Kent
>
> _____
>
> From: junit@yahoogroups. <mailto:junit%40yahoogroups.com> com
[mailto:junit@yahoogroups. <mailto:junit%40yahoogroups.com> com] On Behalf
Of

> Bogdan
> Sent: Friday, September 18, 2009 6:48 AM
> To: junit@yahoogroups. <mailto:junit%40yahoogroups.com> com
> Subject: [junit] TemporaryFolder not usable inside setUp() method
>
>
>
>
> Hello,
>
> I am using JUnit 4.7 and used the new Rules mechanism, with TempFolder.
What
> I needed was to have a temporary folder where the CUT could create some
> folders, that I can then check and assert on them.
>
> The problem that I encountered is that TempFolder is initialized only
AFTER

> my setup method, therefore making it unusable in the set up of the test.
>
> Here is my code:
>
> -----------------------------------------------------
> public class MyClassTest {
> @Rule
> public TemporaryFolder testFolder = new TemporaryFolder();
>
> @Before
> public void setUp() {
> // here testFolder has not been created
> // testFolder.getRoot() is null
> }
>
> @Test
> public void testSomething() {
> // here tempFolder is initialized
> // testFolder.getRoot() is not-null, and points to a temp folder
> }
> }
> -----------------------------------------------------
>
> Is this ok and intended? For me would be much more logical to have the
> TemporaryFolder prepared when the setUp() method is invoked, so that I can
> then set up my mockups and my objects with this test folder. Otherwise, I
> have to place some set up code at the beginning of each test method, which
> basically invalidates the reason for text fixtures.
>
> What do you think?
>
> Regards,
> Bogdan Mocanu
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>






[Non-text portions of this message have been removed]