我在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)
行指向用户名行。有谁知道如何解决它?我是新来的。
答案 0 :(得分:2)
您需要使用@ImportResource注释将XML配置导入到类中。
@RunWith( SpringJUnit4ClassRunner.class )
@Configuration
@ContextConfiguration( classes = { TestClass.class }, loader = AnnotationConfigContextLoader.class )
@ImportResource( { "classpath:resources/applicationConfig.xml" } )
public class TestClass {}
答案 1 :(得分:0)
当Spring自动装配时,它会寻找一个构造函数或“getter”来用于autowire(它也说它是通过字段来完成的,但我从来没有运气过它)。解决问题的最简单方法是执行以下操作:
public class P{
private User mailUser;
@Autowired
public void setUser(User mailUser) {
this.mailUser = mailUser;
}