如何让 Junit 5 暂停其他线程并只执行一个特定的测试类

问题描述 投票:0回答:1

我希望添加

@Execution(ExecutionMode.SAME_THREAD)
才能工作,但事实并非如此。其他测试并行运行。是否有一些注释可以同步测试线程并使它们真正等待?

作为一个例子,我希望所有其他测试都保持到

AaInitialStateITCase
完成为止。但事实并非如此,它并行运行虚拟程序。

所以基本上我希望一些测试单独执行,特别是初始化。 expected to start paused and then wait for slot and pause when another test is standalone

这就是

forkCount = 4
实际发生的情况:

  1. 运行 pl.mol.molnet.initsaas.Dummy2ITCase
  2. 运行 pl.mol.molnet.initsaas.Dummy1ITCase
  3. 运行 pl.mol.molnet.initsaas.AaInitialStateITCase
  4. 运行 pl.mol.molnet.initsaas.Dummy3ITCase
  5. 测试运行:1,失败:0,错误:0,跳过:0,已用时间:27.68 s -- in pl.mol.molnet.initsaas.Dummy1ITCase
  6. 运行 pl.mol.molnet.initsaas.Dummy4ITCase
  7. 测试运行:1,失败:0,错误:0,跳过:0,经过的时间:0.008 s -- in pl.mol.molnet.initsaas.Dummy4ITCase

IT案例示例

/**
 * Init main database objects.
 * @author Maciej Nux Jaros
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration({"/testContextPg.xml"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.SAME_THREAD)   // cannot be run in paralel
public class AaInitialStateITCase extends TestDatabaseCleaner {
    @BeforeAll
    public void mockTenantService() throws SaasException {
        initAll();
    }

    @Test
    public void testInit_shouldInitTenants() throws Exception {
        assertNotNull(tenant1);
        assertNotNull(tenant2);
    }
}

/**
 * Artificial test exhausting threads.
 * @author Maciej Nux Jaros
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration({"/testContextPg.xml"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Dummy1ITCase extends TestDatabaseCleaner {
    @Test
    public void test_forthreads() throws Exception {
        assertEquals(2, 1+1);
    }
}

Maven 配置:

            <!-- ITCase setup -->
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.surefire-failsafe.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skipTests>${skip.integration.tests}</skipTests>
                    <forkCount>4</forkCount>
                    <reuseForks>true</reuseForks>
                    <argLine>-Xms512m -Xmx1280m -Dfile.encoding=UTF-8</argLine>
                    <properties>
                        <configurationParameters>
                            junit.jupiter.execution.parallel.enabled = true
                            junit.jupiter.execution.parallel.mode.default = concurrent
                            junit.jupiter.execution.parallel.mode.classes.default = same_thread
                            # class order to init DB safely
                            junit.jupiter.testclass.order.default = org.junit.jupiter.api.ClassOrderer$ClassName
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
java junit5 junit-jupiter
1个回答
0
投票

如果您使用Junit5,请考虑在测试类上添加@Isolated注释。它确保该类独立运行,并且没有其他类同时运行。

@Isolated
public class myTestClass{
}

这是根据官方 Junit 5 文档:

如果您的大多数测试类可以并行运行而无需任何 同步,但你有一些测试类需要运行 隔离,您可以使用 @Isolated 注解来标记后者。 此类中的测试按顺序执行,无需任何其他操作 测试同时运行。

https://junit.org/junit5/docs/current/user-guide/

© www.soinside.com 2019 - 2024. All rights reserved.