gpt4 book ai didi

测试中的 Spring bean

转载 作者:行者123 更新时间:2023-11-28 21:13:34 25 4
gpt4 key购买 nike

我在 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}" />

我想将它的内容 Autowiring 到我的测试类中

@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)

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

最佳答案

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

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

关于测试中的 Spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com