0

我在 applicationContext.xml 中有一个 Spring bean

    <bean id = "mailUser" class="com.company.application.domain.User">
    <property name="username" value="${mail.username}" />
    <property name="password" value="${mail.password}" />

我想将它的内容自动连接到我的测试类,比如

@Autowired
private User mailUser;

@Test
public void signIn() throws Exception {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://mailservice.com");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.id("username")).sendKeys(getMailUser().getUsername());
    driver.findElement(By.id("password")).sendKeys(getMailUser().getPassword());
    driver.findElement(By.className("btn")).click();

然而,这会导致 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)

行指向用户名行。有谁知道如何正确修复它?我是新手。

4

2 回答 2

2

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

@RunWith( SpringJUnit4ClassRunner.class )
@Configuration
@ContextConfiguration( classes = { TestClass.class }, loader = AnnotationConfigContextLoader.class )
@ImportResource( { "classpath:resources/applicationConfig.xml" } )
public class TestClass {}
于 2012-08-31T09:28:32.490 回答
0

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

public class P{

    private User mailUser;

    @Autowired
    public void setUser(User mailUser) {
        this.mailUser = mailUser;
    }
于 2012-08-31T09:32:50.477 回答