【问题标题】:@Autowired is null in the class which extends Junit Testwatcher@Autowired 在扩展 Junit Testwatcher 的类中为空
【发布时间】:2013-02-27 16:07:20
【问题描述】:

我在我的测试框架中使用 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 扩展了 Testwatcher.class(Junit 规则),它看起来像

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

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


    }

谢谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我没有将 Spring DI 用于 Junit 规则,而是用于我自己的一些类。例如:HTMLLogger 类。这是你要问的。如果不是,请解释为什么我不应该使用 DI。
    【解决方案2】:

    在某些 java 类中,例如测试用例或网络监听器,@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 spring 注释的谷歌。

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

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

    【讨论】:

    • 我必须在不同的类中使用相同的对象。那我怎么得到它。如果我再次手动加载 bean,那么将有 2 个实例。我想在我的框架中使用相同的 bean。
    • 没有。它将是同一个对象,就像您通过 @Autowired 获得的一样。我会在我的编辑中告诉你方法。
    • 我是春天的新手。我很困惑。我在“添加更多信息”下添加了更多信息。请帮助我应该在哪里加载 bean 和重用。
    • 我明白了。好吧,我个人的建议是阅读“spring in action”一书的第一章——你可以在互联网上找到它。首先,尝试理解IOC的概念和bean的本质。
    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2014-03-08
    • 2016-11-12
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多