Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有多个AbstractTestNGSpringContextTests类的Spring引导TestNG_Java_Spring_Spring Mvc_Spring Boot_Testng - Fatal编程技术网

Java 具有多个AbstractTestNGSpringContextTests类的Spring引导TestNG

Java 具有多个AbstractTestNGSpringContextTests类的Spring引导TestNG,java,spring,spring-mvc,spring-boot,testng,Java,Spring,Spring Mvc,Spring Boot,Testng,带有多个AbstractTestNGSpringContextTests类的Spring Boot TestNG根据测试类初始化Spring启动上下文 我定义了多个TestNG测试类,如下所示:- @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @DirtiesContext public class TestNGClass1 extends AbstractTestNGSpringContextTests{ @

带有多个AbstractTestNGSpringContextTests类的Spring Boot TestNG根据测试类初始化Spring启动上下文

我定义了多个TestNG测试类,如下所示:-

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class TestNGClass1 extends AbstractTestNGSpringContextTests{

    @LocalServerPort
    public int port;
    .......
}


有没有办法在所有这些类中重用Spring上下文

@DirtiesContext测试注释,该注释指示 与测试关联的ApplicationContext是脏的,应该 因此,必须将其关闭并从上下文缓存中删除

  • 使用testng套件
  • 删除
    @DirtiesContext
    并使用
    @RunWith(SpringRunner.class)和@SpringBootTest

  • 选中此项

    如何使用@BeforeSuite实现此目标。您能给我一个示例代码段吗?我尝试使用testng套件,而不使用@BeforeSuite检查我的answer@IssamELATIF我正在尝试将现有的应用程序转换为springboot应用程序。如果我使用RunWith annotation,则我得到的端口已被使用。否则,看起来每个testclass都在加载自己的上下文,尽管所有类继承自尝试使用运行集成测试的同一基本classi aamtestng@Vish你能用代码和问题发布你的问题,然后在这篇文章中为我提供评论链接吗。
    @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    @DirtiesContext
    public class TestNGClass2 extends AbstractTestNGSpringContextTests{
    
        @LocalServerPort
        public int port;
        .......
    }
    
    @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    @DirtiesContext
    public class TestNGClass3 extends AbstractTestNGSpringContextTests{
    
        @LocalServerPort
        public int port;
        .......
    }