私はこれにNeedleを使用しています。他のオブジェクトをモックしたい場合は、MockitoとEasyMockでうまくいきます。
まず、私はpersistencteを書きます。私が書く私のJUnitの-TestClassをで
<persistence-unit name="rapPersistenceTest" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/test"/>
...
</properties>
</persistence-unit>
:このようなテストのためのXML(SRC /テスト/リソース/ META-INF)
public class DaoNeedleTest {
//here Needle will create persistenceContext for your testclass
public static DatabaseRule databaseRule = new DatabaseRule("rapPersistenceTest");
//here you can get the entityManager to manipulate data directly
private final EntityManager entityManager = databaseRule.getEntityManager();
@Rule
public NeedleRule needleRule = new NeedleRule(databaseRule);
//here you can instantiate your daoService
@ObjectUnderTest
DAOService daoService;
@Test
public void test() {
//if your method needs a transaction here you can get it
entityManager.getTransaction().begin();
daoService.yourMethod();
entityManager.getTransaction().commit();
}
あなたはまた、/ srcの中に針の構成ファイルを必要としますあなたが使っているMock-providerの種類を教えてください。例えば。私はMockitoを使用しています:
mock.provider=de.akquinet.jbosscc.needle.mock.MockitoProvider
これはそれです。
テストケースはどのように書いていますか? ArquillianやOpenEJBのような組み込みEJBコンテナのような統合フレームワークを使用していますか? –
@PiotrNowicki私は両方を試しました(フレームワークとしてmockitoを使用)。私はそれらを設定する方法、私がインポートする必要があるものはわかりません...とにかく、どんな方法でも問題ありません。 – Simon
私は簡単な例を持っています:http://stackoverflow.com/questions/6469751/testing-an-ejb-with-junit/20635285#20635285私の答えを見てください。 –