如何在Testng的@BeforeTest内部初始化Spring的AnnotationConfigApplicationContext?

huangapple 未分类评论50阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2020年4月10日 19:18:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61139184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定