java8+junit5實現並發測試(多線程)


1.配置線程

 

 

#是否允許並行執行true/false
junit.jupiter.execution.parallel.enabled = true
#是否支持方法級別多線程same_thread/concurrent
junit.jupiter.execution.parallel.mode.default = concurrent
#是否支持類級別多線程same_thread/concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
# the maximum pool size can be configured using a ParallelExecutionConfigurationStrategy
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=10

 

2. 編寫並發測試的代碼

import com.wechat.apiobject.DepartMentObject;
import com.wechat.apiobject.TokenHelper;
import io.restassured.response.Response;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.parallel.Execution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

/**
 * 對創建部門進行並發測試
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Demo_06_1_thread_creatdepartment {
    private static final Logger logger = LoggerFactory.getLogger(Demo_06_1_thread_creatdepartment.class);
    static String accessToken;

    @BeforeAll
    public static void getAccessToken() throws Exception {
        accessToken = TokenHelper.getAccessToken();
        logger.info(accessToken);

    }

    @DisplayName("創建部門")
    @RepeatedTest(100)
    @Execution(CONCURRENT)
    void createDepartment() {
        String creatName= "name1234567";
        String creatEnName="en_name1234567";

        Response creatResponse = DepartMentObject.creatDepartMent(creatName,creatEnName,accessToken);
        assertEquals("0",creatResponse.path("errcode").toString());
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM