Reputation: 1234
I have a Spring bean in applicationContext.xml
<bean id = "mailUser" class="com.company.application.domain.User">
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
I would like to autowire it's contents to my test class like
@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();
This however causes a NullPointerException. I see
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)
Line points to username line. Does anyone know how to fix it right? I'm new to this.
Upvotes: 0
Views: 131
Reputation: 2396
When Spring autowires, it looks for a constructor or "getter" in which to use to autowire (it also says it does it by field, but I've never had any luck with it). Easiest way to fix your problem is to do the following:
public class P{
private User mailUser;
@Autowired
public void setUser(User mailUser) {
this.mailUser = mailUser;
}
Upvotes: 0
Reputation: 330
You will need to import your XML configuration into the class using the @ImportResource annotation.
@RunWith( SpringJUnit4ClassRunner.class )
@Configuration
@ContextConfiguration( classes = { TestClass.class }, loader = AnnotationConfigContextLoader.class )
@ImportResource( { "classpath:resources/applicationConfig.xml" } )
public class TestClass {}
Upvotes: 2