2016-09-02 7 views
-1

シングルトンクラスを作成しました。すべてのシングルトンのように、プライベートコンストラクタの私のシングルトンと、クラスのオブジェクトの定義済みの量を含む静的フィールドで構成されています。IntelliJが1つずつテストを実行しています

この場合は、JSONファイルを使用してシングルトンクラスを初期化する必要があり、期待される動作では、指定されたリストのファイルが存在しない場合に例外がスローされます。

問題は明らかにIntelliJ(あるいはたぶんJUnit)がすべてのテストクラスをロードしてから、それらを1つのプログラムで順不同で実行しているということです。これは、間違った初期化をテストするクラスに達するまでに、すでにシングルトンが初期化されていることを意味します。これは、すべてのテストを一度に実行するたびに、合格しなかった1つのテストを行い、手動で1つずつ実行すると、すべてのテストが合格になります。

IntelliJにすべてのテストクラスを強制的に実行させる方法はありますか?

編集

私はこれが何かを変えることができる方法を知っているが、それはすると仮定することができますしません。ここではまったく同じ動作のサンプルコードを示します。これはIDEの質問であり、Java固有のものではありません。

public class Singleton { 
    private static Singleton singleton = new Singleton(); 

    public static Singleton getInstance() { 
     return singleton; 
    } 

    private Singleton() { 
     if (!new File("maybeIExist.txt").exists()) 
      throw new ExceptionInInitializerError(); 
    } 

    public void doStuff() { 
     System.out.println("stuff"); 
    } 
} 

そしてもちろんテスト:

public class SingletonTestOne { 
    @Test(expected = ExceptionInInitializerError.class) 
    public void test() throws Exception { 
     Singleton.getInstance(); 
    } 
} 

2つ目:

public class SingletonTestTwo { 
    @Before 
    public void before() throws Exception { 
     //code creating file 
    } 
    @Test 
    public void test() throws Exception { 
     Singleton.getInstance(); 
     doStuff(); 
    } 
    @After 
    public void after() throws Exception { 
     //code deleting file 
    } 
} 
+0

は、あなたのシングルトンクラスのコードを貼り付けます。 – progyammer

+0

私はそれを加えました。コードがどのように質問に何を追加したのか分かりませんが、ここではそれがあります。 –

+1

あなたは[this](https://github.com/junit-team/junit4/wiki/test-execution-order)を試してください。 – ahrytsiuk

答えて

1

これはIntelliJの問題ではありませんmvn testを実行しているとき、あなたは同じに遭遇するでしょう。あなたは、以下の変更を実行するテストを得ることができます。

まず、それがsingletonフィールドを遅延getInstance()呼び出しで作成されますように、この方法は今​​にする必要があることがわかり、Singletonクラスを変更します。フィールドをリセットするには、これが必要です。

public final class Singleton { 
    private static Singleton singleton = null; 

    public static synchronized Singleton getInstance() { 
    if (singleton == null) { 
     singleton = new Singleton(); 
    } 
    return singleton; 
    } 

    private Singleton() { 
    if (!new File("maybeIExist.txt").exists()) { 
     throw new ExceptionInInitializerError(); 
    } 
    } 

    public void doStuff() { 
    System.out.println("stuff"); 
    } 
} 

次は、あなたのテストクラスでは、あなたはリフレクションを使用してnull@Before方法でsingletonフィールドをリセットすることができます。

import org.junit.Before; 
import org.junit.Test; 

import java.lang.reflect.Field; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

import static org.junit.Assert.fail; 
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.core.IsNull.notNullValue; 

public class SingletonTest { 

    @Before 
    public void resetSingleton() throws NoSuchFieldException, IllegalAccessException { 
    Field instance = Singleton.class.getDeclaredField("singleton"); 
    instance.setAccessible(true); 
    instance.set(null, null); 
    } 

    @Test 
    public void singletonCanFindFile() throws Exception { 
    final Path path = Paths.get("maybeIExist.txt"); 

    Files.createFile(path); 
    final Singleton instance = Singleton.getInstance(); 
    assertThat(instance, notNullValue()); 
    Files.deleteIfExists(path); 
    } 

    @Test(expected = ExceptionInInitializerError.class) 
    public void singletonCannotFindFile() throws Exception { 
     final Singleton instance = Singleton.getInstance(); 
     fail("no exception thrown!"); 
    } 
} 
関連する問題