英文:
How to initialise Spring AnnotationConfigApplicationContext inside @BeforeTest in Testng?
问题
AppTest.class
@ComponentScan(basePackages = "org.comp.automation")
public class AppTest extends AbstractTestNGSpringContextTests{
@BeforeTest
public void intialise() throws Exception{
ApplicationContext context = new AnnotationConfigApplicationContext(BeanTwo.class);
}
@Autowired
public CommonBean commonBean;
@Autowired
public BeanTwo beanTwo;
@Parameters(value={"param"})
@Test
public void shouldAnswerWithTrue(String param) {
System.out.println(commonBean.STRING_CHECK);
commonBean.createCommonBeanObject(param);
System.out.println("Checking the bean values now...");
assertTrue( true );
}
}
CommonBean.class
@Component
public class CommonBean {
public String commonBeanValue;
public String STRING_CHECK = "Hello!!";
public void createCommonBeanObject(String param){
commonBeanValue = param;
}
public void printParamValue(){
System.out.println(commonBeanValue);
}
}
BeanTwo.class
@Component
public class BeanTwo{
public void printBeanTwo(){
System.out.println("Bean Two!!");
}
}
请问还有其他需要帮助的地方吗?
英文:
I have a testng+spring suite with parallel option set to Tests. I would like to have each <test>
have it's own ApplicationContext. So each <test>
thread will have their own set of beans. I am able to initialise the ApplicationContext but the beans are not getting autowired and I am getting nullpointer exception when I am trying to access the bean. Please find my code below,
AppTest.class
@ComponentScan(basePackages = "org.comp.automation")
public class AppTest extends AbstractTestNGSpringContextTests{
@BeforeTest
public void intialise() throws Exception{
ApplicationContext context = new AnnotationConfigApplicationContext(BeanTwo.class);
}
@Autowired
public CommonBean commonBean;
@Autowired
public BeanTwo beanTwo;
@Parameters(value={"param"})
@Test
public void shouldAnswerWithTrue(String param) {
System.out.println(commonBean.STRING_CHECK);
commonBean.createCommonBeanObject(param);
System.out.println("Checking the bean values now...");
assertTrue( true );
}
}
CommonBean.class
@Component
public class CommonBean {
public String commonBeanValue;
public String STRING_CHECK = "Hello!!";
public void createCommonBeanObject(String param){
commonBeanValue = param;
}
public void printParamValue(){
System.out.println(commonBeanValue);
}
}
BeanTwo.class
@Component
public class BeanTwo{
public void printBeanTwo(){
System.out.println("Bean Two!!");
}
}
Can someone help me with this and point me in the right direction?
专注分享java语言的经验与见解,让所有开发者获益!
评论