@Autowired在扩展Junit Testwatcher的类中为null

时间:2013-02-27 16:07:21

标签: java spring java-ee spring-mvc junit

我在测试框架中使用Spring自动扫描组件。

Autowired在所有类中都可以正常工作,但在扩展Junit Testwatcher的类中除外。

我扩展Junit testwatcher的类是:

@Component
public class PrintTrace extends TestWatcher{//this class overrides pass and fail   
                                            //methods in Testwatcher           

@Autowired
private HTMLLogger htmlLogger //this is null all the time. it works in 
                              //other classes
}

My Base类看起来像:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:beans.xml"})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class })
public class YahooTestSetUp {

    public static WebDriver driver;


    @Rule
    public PrintTrace printTrace = new PrintTrace();



    @Autowired
    private HTMLLogger htmlLogger;//here its working fine
}

我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.pilz"/>
</beans>

请建议这对我来说非常重要。否则我应该去 单身对象,对我来说不是一个好的解决方案。


添加更多信息:

假设我有2个测试套件可以访问2个公共类HTMLLogger.class和PrintTraece.class。

每个测试套件都独立于其他套件。这些套件有@BeforeClass和@AfterClass

见下文:

@RunWith(Suite.class)
@Suite.SuiteClasses({ AutowireClass.class})
public class YahooTestSuite extends YahooTestSetUp{
    private static HTMLLogger htmlLogger;

    @BeforeClass
    public static void allTests() throws Exception {**//Can I use @Autowired in
                                                      static methods(no)**  

        htmlLogger = new HTMlLogger()


        htmlLogger.createHTMLLogFile();
    }

    @AfterClass
    public static void afterAll() throws Exception{
        htmlLogger.htmlLogEnd();
    }


}

另一个套件正在为其他模块做同样的事情。

正如我之前所说,我的PrintTrace.class扩展了Testwatcher.class(Junit规则),它看起来像

使用在baseclass中创建的规则调用此类。

@Component
public class PrintTrace extends TestWatcher{

    private HTMLLogger htmlLogger = null;
@Override
    public void starting(final Description description) {

    }

    @Override
    public void failed(final Throwable e, final Description description) {
        htmlLogger.writeFailLog();**//This is common for all suites**
                                        //This should be same object used in suite
    }

    @Override
    public void succeeded(final Description description) {

        htmlLogger.writePassLog();//This is common for all suites
                                      //This should be same object used in suite
    }

    @Override
    public void finished(Description description) {


    }

感谢

2 个答案:

答案 0 :(得分:0)

在某些java类中,如测试用例或Web侦听器,@Autowired不起作用。您必须像对象一样定义applicationContext并手动接收bean。

ApplicationContext context = AnnotationConfigApplicationContext("app_context_file_path")

HTMLLogger htmlLogger = context.getBean("loggerBeanName");

另外,请查看此处 - http://static.springsource.org/spring/docs/3.0.x/reference/testing.html

关于@Resource春季注释的谷歌。

要获得所需的确切对象,您应该具有以下配置:

如果在应用程序上下文文件中创建bean,只需在getBean(name)方法中使用它的id。如果将它作为单独的类提供,请为其(类)提供@Component(value)注释,并在应用程序上下文中扫描包。之后,使用该bean(组件)的值名称来获取您已配置的确切对象。

答案 1 :(得分:0)

为什么要将Spring DI用于JUnit规则?您的测试代码不是您的应用程序。