Search the web
Sign In
New User? Sign Up
junit · JUnit, the Java unit testing framework written by Kent Beck and Erich Gamma.

Group Information

  • Members: 19232
  • Category: Java
  • Founded: Nov 6, 2000
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Messages

  Messages Help
Advanced
Using @Rule TemporaryFolder in @BeforeClass   Message List  
Reply Message #22516 of 23813 |
@Rule objects are not initialized before @BeforeClass methods are called. Is
this by design or a bug?

For example:

package test;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TemporaryFolderRuleBeforeClass {
@Rule
public static TemporaryFolder testFolder = new TemporaryFolder();

@BeforeClass
public static void testInTempFolder() throws IOException {
File tempFile = testFolder.newFile("file.txt");
File tempFolder = testFolder.newFolder("folder");
System.out.println("Test folder: " + testFolder.getRoot());
// test
Assert.assertNotNull(testFolder.getRoot());
}

@Test
public void test() {
// noop
}
}

Having the TemporaryFolder Rule declared as static is OK as this example test
shows:

public class TestTemporaryFolderRuleStatic {
@Rule
public static TemporaryFolder testFolder = new TemporaryFolder();

@Test
public void testInTempFolder() throws IOException {
File tempFile = testFolder.newFile("file.txt");
File tempFolder = testFolder.newFolder("folder");
System.out.println("Test folder: " + testFolder.getRoot());
// test
Assert.assertNotNull(testFolder.getRoot());
}
}

How can @Rules be used from @BeforeClass?

Calling TemporaryFolder.create() would work only for TemporaryFolder and not
Rules in general but would be against the advice of create method Javadoc.




Tue Mar 9, 2010 5:43 pm

gary_d_gregory
Offline Offline
Send Email Send Email

Message #22516 of 23813 |
Expand Messages Author Sort by Date

@Rule objects are not initialized before @BeforeClass methods are called. Is this by design or a bug? For example: package test; import java.io.File; import...
gary_d_gregory Offline Send Email Mar 9, 2010
5:54 pm

Hmmm... I don't think we ever tried @Rule on a static variable before. I'm not even sure what they mean. Rules are inserted before an individual test is run....
Kent Beck
kentlbeck Offline Send Email
Mar 9, 2010
6:59 pm

I'm all for a Rule before the class is run, but I think it is way to gimmicky and obscure to hang that on if the variable is declared static. I would instead...
yme0987654321 Offline Send Email Mar 10, 2010
12:02 am

This is an interesting design tradeoff. I don't like implicit magic but I also don't like creating new elements (like the "beforeClass = true") if I can reuse...
Kent Beck
kentlbeck Offline Send Email
Mar 10, 2010
1:09 am

... Or, by (almost) symmetry, the following? @Before public void... @After public void... @Rule public MethodRule... @BeforeClass public static void... ...
David Saff
dsaff Offline Send Email
Mar 10, 2010
1:56 am

... Sounds like issue #29 @BeforeClass Rule: http://github.com/KentBeck/junit/issues#issue/29 Please vote it up if you feel you need it. Which reminds me to...
Alistair Israel
aisrael Offline Send Email
Mar 10, 2010
3:58 am

WRT: @RuleOnClass public static ClassRule... @ClassRule public static ClassRule... @ClassRule reads better to me than @RuleOnClass and the symmetry with...
Gary
gary_d_gregory Offline Send Email
Mar 10, 2010
4:53 am

I like the idea of an explicit @ClassRule. Static alone is too implicit, I think. Greetings, Malte...
Malte Finsterwalder
maltefinster... Offline Send Email
Mar 10, 2010
8:50 am

I'm agnostic with regards to name (although I reserve my right to hate whatever you pick as a matter of principle), but I agree that having a static @Rule be...
Clive Evans
cliveevans Offline Send Email
Mar 10, 2010
10:49 am

Unfortunately, the same class can't be both an annotation and a type. Further suggestions? David...
David Saff
dsaff Offline Send Email
Mar 10, 2010
2:22 pm

Not sure if this applies, it just popped into my head. Since this is a brain storming session, I wanted to chime in. Would there be a case for @Rule to be...
Forsberg, Mike
mike.forsber... Offline Send Email
Mar 10, 2010
4:43 pm

On Wed, Mar 10, 2010 at 11:40 AM, Forsberg, Mike ... Quite possibly. I can imagine creating a @ParameterSetRule annotation to the Parameterized runner to...
David Saff
dsaff Offline Send Email
Mar 10, 2010
4:52 pm

What does ClassRule clash with? Alternates: - StaticRule - PerClassRule - OneTimeRule - SingletonRule more? Gary...
Gary
gary_d_gregory Offline Send Email
Mar 10, 2010
11:00 pm

... You wrote @ClassRule public static ClassRule..., which can't be valid Java. Does that answer the question? David...
David Saff
dsaff Offline Send Email
Mar 10, 2010
11:02 pm

Ah, right bad example, which should not invalidate the ClassRule rule name....
Gary
gary_d_gregory Offline Send Email
Mar 11, 2010
2:26 am

So, summarizing the proposed solutions: - Add an element to @Rule, for example @Rule(beforeClass=true) - Add a new rule that is initialized before...
Gary
gary_d_gregory Offline Send Email
Mar 11, 2010
8:17 pm

Are any of these solutions acceptable? If no one in JUnit-land has time for this, please let me know. If one of these solutions is acceptable, I could take the...
Gary
gary_d_gregory Offline Send Email
Mar 16, 2010
3:59 pm

Gary, ... The only questions in my mind are the name of the annotation, and the name of the type of the annotated field, since they can't both be BeforeClass....
David Saff
dsaff Offline Send Email
Mar 16, 2010
4:05 pm

Oh yeah, undocumented code styles that may catch you are: * Use tabs instead of spaces * If you optimize imports, make sure that the java.*...
Loritsch, Berin C.
bloritsch Offline Send Email
Mar 16, 2010
4:20 pm

If you use Eclipse, these styles are enforced in the project. If you use another IDE, and can contribute settings files, please do. Thanks, David Saff On Tue,...
David Saff
dsaff Offline Send Email
Mar 17, 2010
6:38 pm

My forked copy of JUnit has the project/style files for JetBrains IDEA. From: junit@yahoogroups.com [mailto:junit@yahoogroups.com] On Behalf Of David Saff ...
Loritsch, Berin C.
bloritsch Offline Send Email
Mar 17, 2010
6:43 pm

I do use Eclipse :) Where do I create a ticket to attach a patch file? Gary...
Gary
gary_d_gregory Offline Send Email
Mar 20, 2010
10:52 pm

JUnit is using GitHub... if you'd like to submit a patch, fork the source at http://github.com/KentBeck/junit... Then send a pull request to dsaff and ...
Mike Forsberg
bigmike_f Offline Send Email
Mar 21, 2010
11:50 am

Some context: In this test, we want to configure a server, start it, run tests, shutdown the server and clear any evidence left on disk. I was hoping this...
Gary
gary_d_gregory Offline Send Email
Mar 10, 2010
1:33 am

Gosh, that almost looks like a test :-) Kent ... [Non-text portions of this message have been removed]...
Kent Beck
kentlbeck Offline Send Email
Mar 10, 2010
1:39 am

Ah! "Almost" is the key word since it does not work! :) Yet. All joking aside, @Rule (TemporaryFolder or any rule) is a nice idiom, it would be nice to get it...
Gary
gary_d_gregory Offline Send Email
Mar 10, 2010
4:27 am
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help