2016-05-25 17 views
0

私は、コントラクトを定義するインタフェース(つまり、Repository)を実装していますが、実装はほとんどありません。インターフェイスの各メソッドは機能を表しています。スイートテストクラスの各機能をテストしたいと思います。スイートでのJUnit契約テスト

次のようにのはUserRepositoryインタフェースを想定してみましょう:

public interface UserRepository { 

    Set<User> search(String query); 

    Set<User> findBySomethingSpecific(String criteria1, Integer criteria2); 
} 

瞬間、私は同じテストケースを実行することを確認するために、私は抽象テストクラスを作成し、私の実装の各が延びてテストクラスを持っています抽象クラス。

public abstract UserRepositoryTest { 

    private UserRepository userRepository; 

    @Before 
    public void setUp() { 
     userRepository = createUserRepository(); 
    } 

    @Test public void aTestForSearch() { ... } 
    @Test public void anotherTestForSearch() { ... } 

    @Test public void aTestForSomethingSpecific() { ... } 
    @Test public void anotherTestForSomethingSpecific() { ... } 

    protected abstract UserRepository createUserRepository(); 
} 

//------------------------ 

public class UserRepositoryImplementationTest extends UserRepositoryTest { 

    @Override 
    protected UserRepository createUserRepository() { 
     return new UserRepositoryImplementation(); 
    } 
} 

テストクラスが急速に圧倒なったので、私は、小さな一連のテストには、この抽象テストクラスを分割する方法を見つけるしたいと思います。私はテストスイートを見てきましたが、私は私の異なる実装をインジェクションしてスイートテストクラスを作る方法を理解できません。

私はこのquestionを見つけましたが、私のリポジトリには作成時にいくつかのロジックが必要です(SQL実装ではConnectionPoolなど)。私は現在、異なるContextクラスのアンチパターンServiceLocatorを使用して、作成を処理しますが、これはstaticです。だからこそ私はテストクラスを実装してアプローチを取っているので、コンテキストを作成して後で注入することができます。完全な例はhere

アカウントに持っている必要があります別のものを見つけることができ

import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 

@RunWith(Suite.class) 
@Suite.SuiteClasses({ 
    TestFeatureLogin.class, 
    TestFeatureLogout.class, 
    TestFeatureNavigate.class, 
    TestFeatureUpdate.class 
}) 

/** 
* 
* This suite will execute TestFeatureLogin,TestFeatureLogout,TestFeatureNavigate and TestFeatureUpdate one after the over. 
* 
* @Before, @After and @Test are no possible of executing in this class. 
* @BeforeClas and @AfterClass are allowed only. 
* 
* */ 
public class FeatureTestSuite { 
    // the class remains empty of test,although it is possible set up a before class and after class annotations. 
    // used only as a holder for the above annotations 

    @BeforeClass 
    static public void beforeClass(){ 
     System.out.println(FeatureTestSuite.class.toString() + " BeforeClass Method"); 
    } 

    @AfterClass 
    static public void AfterClass(){ 
     System.out.println(FeatureTestSuite.class.toString() + " AfterClass Method"); 
    } 
} 

@Testは良い習慣であるとされていない:

答えて

0

聖霊降臨祭JUnitの4あなたは、このようなスイートを作成することができます抽象クラス内のユニットテストの実装をテストする場合は、抽象クラスを継承するテストクラスを作成します。

+0

'FeatureTestSuite'はすべてのスイートクラスにどのように依存関係を注入しますか? – MiniW

+0

@MiniW別のオプションは、Junit Rules http://stackoverflow.com/a/13489506/1371064とJUnitパラメータを使用しています。 Obviosly、これはテストしたいテストの種類によります。私の場合、私は常にSpringとそのコンテキストとの依存関係を使用します。ここでは、http://stackoverflow.com/a/14946430/1371064の例を示します –

関連する問題