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.
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.
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.
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.