Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Spring 测试中的弹簧豆_Spring_Testing_Webdriver - Fatal编程技术网

Spring 测试中的弹簧豆

Spring 测试中的弹簧豆,spring,testing,webdriver,Spring,Testing,Webdriver,我在applicationContext.xml中有一个Springbean <bean id = "mailUser" class="com.company.application.domain.User"> <property name="username" value="${mail.username}" /> <property name="password" value="${mail.password}" /> 但是,这会

我在applicationContext.xml中有一个Springbean

    <bean id = "mailUser" class="com.company.application.domain.User">
    <property name="username" value="${mail.username}" />
    <property name="password" value="${mail.password}" />
但是,这会导致NullPointerException。我明白了

    java.lang.NullPointerException
at com.company.app.SignInTest.signIn(SignInTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

行指向用户名行。有人知道怎么把它修好吗?我是新手。

您需要使用@ImportResource注释将XML配置导入到类中

@RunWith( SpringJUnit4ClassRunner.class )
@Configuration
@ContextConfiguration( classes = { TestClass.class }, loader = AnnotationConfigContextLoader.class )
@ImportResource( { "classpath:resources/applicationConfig.xml" } )
public class TestClass {}

当Spring自动连线时,它会寻找一个构造函数或“getter”,用于自动连线(它还说它是按字段进行的,但我从来没有用过它)。解决问题的最简单方法是执行以下操作:

public class P{

    private User mailUser;

    @Autowired
    public void setUser(User mailUser) {
        this.mailUser = mailUser;
    }

NullPointerException
的完整堆栈跟踪是什么?哪一行?获取用户名行。我认为它根本无法从bean中获取值。它可以在该行的至少4个位置抛出NPE,问题编辑+完整堆栈跟踪真的很受欢迎…您是否必须将applicationContext放置在特定的位置?我把它放在WEB-INF下。它需要从类路径访问。在我的示例中,我将applicationContext.xml文件放在resource/下,因为现在很多文档都是这样做的。对于测试用例,我将特定于XML的配置放在与测试源文件相同的位置,在这种情况下,您应该能够通过“classpath:applicationContext.XML”引用它。我将它们放在同一个包中,但仍然得到一个错误,即XML文件不存在。我肯定我打对了。对不起,我的评论错了!如果执行
@import({“applicationContext.xml”})
applicationContext.xml
应位于包树的根目录下。如果您希望按照我的建议将
applicationContext.xml
存储在与测试类相同的包中,则需要放置完整的包路径。例如,
uk.me.bjw.tests.MyTest.class
将有一个
@ImportResource({“uk/me/bjw/tests/applicationContext.xml})
。您知道从Netbeans测试包导入资源的路径吗?
public class P{

    private User mailUser;

    @Autowired
    public void setUser(User mailUser) {
        this.mailUser = mailUser;
    }