gpt4 book ai didi

Java Testng 单元测试 : Spring is not able to create object before BeforeSuite or BeforeTest

转载 作者:行者123 更新时间:2023-12-02 01:30:12 26 4
gpt4 key购买 nike

我有一个单元测试在当前格式下运行良好。我的单元测试如下所示:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = { "classpath:testContext.xml" })
public class DialPadMonitorTest extends AbstractTestNGSpringContextTests
{
@Autowired
DialPadMonitor service;


@BeforeSuite
public void doBeforeSuite() {
System.out.println("Initializing suit.........");
}

@BeforeTest
public void doBeforeTest() {
System.out.println("Initializing tests.........");
}

@Test()
void testIfEasyToDial()
{
service.createDialAssociation();

Assert.assertTrue(service.isNumberEasy(159658521));
Assert.assertTrue(service.isNumberEasy(555555555));
Assert.assertFalse(service.isNumberEasy(505555555));
Assert.assertFalse(service.isNumberEasy(555555505));

Assert.assertTrue(service.isNumberEasy(2547096));
Assert.assertTrue(service.isNumberEasy(5547521));
Assert.assertFalse(service.isNumberEasy(2806547));
Assert.assertFalse(service.isNumberEasy(3558123));

}

@Test()
void testIfAssociated()
{
service.createDialAssociation();

Assert.assertTrue(service.isNoAssociated(3,3));
Assert.assertTrue(service.isNoAssociated(3,6));
Assert.assertFalse(service.isNoAssociated(3,9));

}
}

即使我看到这样的评论

  • System.out.println("正在初始化套装......");
  • System.out.println("正在初始化测试......");

问题是,如果我将 service.createDialAssociation() 移动到 BeforeSuite 或 BeforeTest 中的任何一个中,我会收到空​​指针异常。

对于在这种情况下为什么会出现空指针异常以及如何解决有什么见解吗?

最佳答案

您应该使用@BeforeMethod而不是@BeforeTest,因为上下文是以这种方式初始化的。您可以在 Spring source file 中看到它.

关于Java Testng 单元测试 : Spring is not able to create object before BeforeSuite or BeforeTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56209736/

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