Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @Autowired在扩展Junit Testwatcher的类中为null_Java_Spring_Jakarta Ee_Spring Mvc_Junit - Fatal编程技术网

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

Java @Autowired在扩展Junit Testwatcher的类中为null,java,spring,jakarta-ee,spring-mvc,junit,Java,Spring,Jakarta Ee,Spring Mvc,Junit,我正在测试框架中使用Spring autoscan组件 Autowired在所有类中都可以正常工作,除了在扩展JUnitTestWatcher的类中 我扩展Junit testwatcher的类是: @Component public class PrintTrace extends TestWatcher{//this class overrides pass and fail //methods i

我正在测试框架中使用Spring autoscan组件

Autowired在所有类中都可以正常工作,除了在扩展JUnitTestWatcher的类中

我扩展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>
另一个套件也在为其他模块做同样的事情

正如我前面所说,我的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类中,如测试用例或web侦听器,
@Autowired
不起作用。您必须像对象一样定义applicationContext并手动接收bean

ApplicationContext context = AnnotationConfigApplicationContext("app_context_file_path")

HTMLLogger htmlLogger = context.getBean("loggerBeanName");
还有,看看这里-

和谷歌关于
@Resource
spring注释

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


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

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

我不是将Spring DI用于Junit规则,而是用于我自己的一些类。例如:HTMLLogger类。这就是你要问的吗。如果不是,请解释为什么我不应该使用DI。我必须在不同的类中使用相同的对象。那我怎么得到它呢。如果我再次手动加载bean,那么将有2个实例。我想在我的框架中使用相同的bean。它将是同一个对象,就像您通过@Autowired获得它一样。我将在我的编辑中为你指路。我是春天的新手。我很困惑。我在“添加更多信息”下添加了更多信息。请帮助我在哪里加载bean并重用。我明白了。嗯,我个人的建议是阅读《春天在行动》一书的第一章——你可以在互联网上找到它。首先,试着理解国际奥委会的概念和本质。
ApplicationContext context = AnnotationConfigApplicationContext("app_context_file_path")

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