0

我在我的测试框架中使用 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
}

我的基类看起来像:

@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 extends Testwatcher.class(Junit rule) 它看起来像

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

@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) {


    }

谢谢

4

2 回答 2

0

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

于 2013-02-27T18:19:33.283 回答
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(组件)的值名称来获取您配置的确切对象。

于 2013-02-27T17:02:07.260 回答