what's the difference between @beforeGroups and @beforeClass

345 views
Skip to first unread message

smokingcat

unread,
Mar 14, 2007, 4:20:39 AM3/14/07
to testng-users
Hey, TestNG gurus. I am a testng newbe.

I wrote some code for running some spring tests, it may look like
this:

public class SpringTests {
protected ApplicationContext ctx ;

//This is the question: ....
@BeforeGroup(groups = { "spring" })

public void initialApplicationContext() {
if (ctx == null) {
this.ctx = new ClassPathXmlApplicationContext(
"classpath:/spring/**/*.xml");
System.err.println("********************************************");
System.err.println(" Application Context initialized! ");
System.err.println("********************************************");
}
}
}
public class VoiceNoteDetailDaoTest extends SpringTests {
@BeforeClass(groups = { "spring" })
public void setup() {
service = (VoiceNoteService) ctx.getBean("VoiceNoteService",
VoiceNoteService.class);
}
...
...
}

public classXXXextends SpringTests {
@BeforeClass(groups = { "spring" })
public void setup() {
service = ... }
...
...
}


If the initialApplicationContext funcation has been marked with
@BeforeGroup annotation, the setup() will throw Null Exception tell me
the ctx is null and may need to be initialized first.

if I mark the initialApplicationContext funcation with @BeforeClass,
the setup() will run perfectly.

but when I run the spring test group , the applicationContext will be
initialize twice.

Anyone can tell me the reson and give me some advice?

Thank you.

Cédric Beust ♔

unread,
Mar 14, 2007, 7:23:28 AM3/14/07
to testng...@googlegroups.com
Hi Smokingcat,

The reason why you're seeing this is because you are initializing your ctx variable in a @BeforeClass method but you have two test classes, therefore this method gets called twice.

If you want to have an initialization method that is run once for several classes, you need to put these classes in a <test>:

<suite...

  <test>
    <classes>
      <class name="VoiceNoteDetailDaoTest" />
      <class ...>
    </classes>
  </test>

And then put your initialization in the base class of all these tests in a @BeforeTest method:

public class SpringTests {
       protected  ApplicationContext ctx ;

       @BeforeTest

       public void initialApplicationContext() {
               if (ctx == null) {
                        this.ctx = new ClassPathXmlApplicationContext
                       ...
}

Let me know if this fixes your problem!

--
Cedric





On 3/14/07, smokingcat <fanw...@gmail.com> wrote:

Hey, TestNG gurus. I am a testng newbe.

I wrote some code for running some spring tests, it may look like
this:

public class SpringTests {
        protected  ApplicationContext ctx ;

                //This is the question: ....
        @BeforeGroup(groups = { "spring" })

        public void initialApplicationContext() {
                if (ctx == null) {
                        this.ctx = new ClassPathXmlApplicationContext(
                                        "classpath:/spring/**/*.xml");
                        System.err.println("********************************************");
                        System.err.println (" Application Context initialized! ");

                        System.err.println("********************************************");
                }
        }
}
public class VoiceNoteDetailDaoTest extends SpringTests {
           @BeforeClass(groups = { "spring" })
        public void setup() {
                service = (VoiceNoteService) ctx.getBean("VoiceNoteService",
                                VoiceNoteService.class );
        }
          ...
          ...
}

public classXXXextends SpringTests {
           @BeforeClass(groups = { "spring" })
        public void setup() {
                service = ...   }
          ...
          ...
}


If the initialApplicationContext funcation has been marked with
@BeforeGroup annotation, the setup() will throw Null Exception tell me
the ctx is null and may need to be initialized first.

if I mark the initialApplicationContext funcation with @BeforeClass,
the setup() will run perfectly.

but when I run the spring test group , the applicationContext will be
initialize twice.

Anyone can tell me the reson and give me some advice?

Thank you.





--
Cédric

范武

unread,
Mar 14, 2007, 10:15:46 PM3/14/07
to Cédric Beust ♔

Hi Cedric,

       

       Thank for your help.

       

       I used the @BeforeTest annotation, but only the first run test class will run, the others will thrown NullPointerException.


       I think I may know where the problem is, I modify ApplicationContext field to static, and it works well. 


public class SpringTests {

       protected static ApplicationContext ctx ;


       @BeforeTest

       public void initialApplicationContext() {

               if (ctx == null) {

                        this.ctx = new ClassPathXmlApplicationContext

                       ...

}


       But if I change the annotation to @BeforeGroup, only the first class will run, all the other test class will throw NullPointerException, tell me the ctx field may not have been initialized properly.



public class SpringTests {

       protected static  ApplicationContext ctx ;


       @BeforeGroup(groups={"spring"})

       public void initialApplicationContext() {

               if (ctx == null) {

                        this.ctx = new ClassPathXmlApplicationContext

                       ...

}


Thank you.

Cédric Beust ♔

unread,
Mar 15, 2007, 5:35:07 AM3/15/07
to testng...@googlegroups.com
On 3/15/07, 范武 <fanw...@gmail.com> wrote:

Hi Cedric,

      

       Thank for your help.

       

       I used the @BeforeTest annotation, but only the first run test class will run, the others will thrown NullPointerException.

       I think I may know where the problem is, I modify ApplicationContext field to static, and it works well. 

Yes, considering your design, this seems unavoidable:  you want to  initialize a variable that should be available across several classes, so a static is probably the most direct way to achieve this.

--
Cédric
Reply all
Reply to author
Forward
0 new messages