Junit如何执行测试用例?

huangapple go评论86阅读模式
英文:

How does Junit execute the test cases?

问题

我正在努力理解如何使JUnit线程安全我有一个静态类在整个代码库中如下使用

public class FaultList {
    private static final String MESSAGE_CONTEXT_KEY = "FaultList";
    private static ThreadLocal<ArrayList<Fault>> threadFaultList = new ThreadLocal();
    private static final String[] ACCOUNT_NOT_FOUND_STRINGS = new String[]{"INVALID CARDHOLDER NUMBER", "INVALID CHECK DIGIT ENTERED", "RECORD NOT FOUND", "DATA REQUESTED IS RESTRICTED FOR TERMINAL"};

    public FaultList() {
    }

    public static void init() {
        threadFaultList.set(new ArrayList());
        MessageContext.getMessageContext().put("FaultList", Collections.synchronizedList(new ArrayList()));
    }

    // ... 其他方法 ...

    public static Fault getSevereFaultFromFaultList() {
        Iterator var0 = getList().iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return null;
            }

            f = (Fault)var0.next();
        } while(!f.getSeverity().getValue().equalsIgnoreCase("ERROR") && !f.getSeverity().getValue().equalsIgnoreCase("CRITICAL_ERROR"));

        return f;
    }
}

对于您提到的在同一线程中执行的两个方法,Java会按照顺序执行它们。在单个线程中,方法A将在方法B之前执行,不会并发地交错执行。因此,在您的示例中,不会出现并发问题。每个方法都会在自己的线程上运行,线程内部使用的是独立的threadFaultList,因此不会干扰彼此的操作。

英文:

I am trying to wrap my head around making junits threadsafe. I have a static class that is used across the entire codebase like so:

public class FaultList {
    private static final String MESSAGE_CONTEXT_KEY = &quot;FaultList&quot;;
    private static ThreadLocal&lt;ArrayList&lt;Fault&gt;&gt; threadFaultList = new ThreadLocal();
    private static final String[] ACCOUNT_NOT_FOUND_STRINGS = new String[]{&quot;INVALID CARDHOLDER NUMBER&quot;, &quot;INVALID CHECK DIGIT ENTERED&quot;, &quot;RECORD NOT FOUND&quot;, &quot;DATA REQUESTED IS RESTRICTED FOR TERMINAL&quot;};

    public FaultList() {
    }

    public static void init() {
        threadFaultList.set(new ArrayList());
        MessageContext.getMessageContext().put(&quot;FaultList&quot;, Collections.synchronizedList(new ArrayList()));
    }

    public static boolean isFDRExceptionAccountNotFound(String errorMessage) {
        for(int i = 0; i &lt; ACCOUNT_NOT_FOUND_STRINGS.length; ++i) {
            if (errorMessage.indexOf(ACCOUNT_NOT_FOUND_STRINGS[i]) != -1) {
                return true;
            }
        }

        return false;
    }

    public static boolean isEmpty() {
        return ((ArrayList)threadFaultList.get()).isEmpty();
    }

    public static void add(Fault fault) {
        ((ArrayList)threadFaultList.get()).add(fault);
        getMessageContextFaults().add(fault);
    }

    public static void addAll(Collection&lt;? extends Fault&gt; faults) {
        ((ArrayList)threadFaultList.get()).addAll(faults);
        getMessageContextFaults().addAll(faults);
    }

    public static List&lt;Fault&gt; getMessageContextFaults() {
        return (List)MessageContext.getMessageContext().get(&quot;FaultList&quot;);
    }

    public static ArrayList&lt;Fault&gt; getList() {
        return (ArrayList)threadFaultList.get();
    }

    public static boolean isSevereErrorPresent() {
        Iterator var0 = ((ArrayList)threadFaultList.get()).iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return false;
            }

            f = (Fault)var0.next();
        } while(!f.getSeverity().getValue().equalsIgnoreCase(&quot;ERROR&quot;) &amp;&amp; !f.getSeverity().getValue().equalsIgnoreCase(&quot;CRITICAL_ERROR&quot;));

        return true;
    }

    public static boolean isPaymentDeniedFaultPresent() {
        Iterator var0 = ((ArrayList)threadFaultList.get()).iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return false;
            }

            f = (Fault)var0.next();
        } while(!f.getId().getName().contains(&quot;3179&quot;) &amp;&amp; !f.getId().getName().contains(&quot;3180&quot;) &amp;&amp; !f.getId().getName().contains(&quot;3184&quot;));

        return true;
    }

    public static boolean isAddressParsingFaultPresent() {
        Iterator var0 = ((ArrayList)threadFaultList.get()).iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return false;
            }

            f = (Fault)var0.next();
        } while(!f.getId().getName().contains(&quot;3500&quot;));

        return true;
    }

    public static boolean isMemoPostFailedPresent() {
        Iterator var0 = ((ArrayList)threadFaultList.get()).iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return false;
            }

            f = (Fault)var0.next();
        } while(!f.getId().getName().contains(&quot;3069&quot;));

        return true;
    }

    public static boolean isCreditCardTypeWarningPresent() {
        Iterator var0 = ((ArrayList)threadFaultList.get()).iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return false;
            }

            f = (Fault)var0.next();
        } while(!f.getId().getName().contains(&quot;3173&quot;));

        return true;
    }

    public static Fault getSevereFaultFromFaultList() {
        Iterator var0 = getList().iterator();

        Fault f;
        do {
            if (!var0.hasNext()) {
                return null;
            }

            f = (Fault)var0.next();
        } while(!f.getSeverity().getValue().equalsIgnoreCase(&quot;ERROR&quot;) &amp;&amp; !f.getSeverity().getValue().equalsIgnoreCase(&quot;CRITICAL_ERROR&quot;));

        return f;
    }
}

The class is written by using ThreadLocal which binds the faultList to every single thread so junits executed in separate threads do not interfere with each other. What happens to the junits executed in the same thread written as follow?

public void A(){
        FaultList.init();
        FaultList.add(fault1);
} 

public void B(){
     FaultList.init()
     FaultList.add(faul2);
}

Will java execute these junits in the same thread sequentally? or it will for example execute both by taking an instruction from each at a time hence creating a concurrency issue?

答案1

得分: 3

默认情况下,Junit以确定但不可预测的顺序执行测试用例。

以下是一些不错的阅读材料:

英文:

By default Junit is executing test cases in deterministic, but unpredictable order.

Here are some good reads:

答案2

得分: 1

默认情况下,JUnit 在一个线程中运行测试,即按顺序运行。然而,有方法可以在并行线程中运行测试和特定的测试方法。对于 JUnit 5,可以通过配置参数来切换此功能。

英文:

By default JUnit runs tests in one thread, i.e. sequentually. However, there are ways to run tests and test-methods in particular in parallel threads. For JUnit 5 this feature can be switched just by configuration parameters.

huangapple
  • 本文由 发表于 2020年8月18日 03:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63457712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定