2016-07-22 22 views
0

私のプロジェクトでは、私はメモリ内テストクラスを書く必要があるためにリポジトリクラスを作成しました。私のリポジトリコードは以下の通りです。Springデータjpaリポジトリインメモリテストケース

package org.jaap.reference.repository; 

import java.util.List; 

import org.springframework.cache.annotation.Cacheable; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.Query; 
import org.springframework.data.querydsl.QueryDslPredicateExecutor; 
import org.springframework.stereotype.Repository; 

import org.jaap.entity.AccountType; 

/** 
* Repository for type 
* 
*/ 
@Repository 
public interface AccountTypeRepository 
    extends JpaRepository<AccountType, Integer>, QueryDslPredicateExecutor<Type> { 
/** 
* @param AccountTypeCode 
* @return List<Type> 
*/ 

@Query("select T from AccountType T where T.AccountTypeCode not in ?# {@environment.getProperty('commit.types').split(',')}") 
List<AccountType> findByAccountTypeCodeNotIn(); 

} 

これは私がjunitを使ってユニットテストケースを書く必要があります、誰かが私を助けることができますか?

答えて

0

我々はダービーでのインメモリデータベース接続を作成することにより、インメモリーテストケースをachiveことができ、私はダービーで行っている、私のコード

テストクラス

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertNotNull; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes=AccountTypeConfiguration.class) 
public class AccountTypeServiceTest { 
    private AccountTypeService accountTypeService; 

    @Autowired 
    private AccountTypeRepository accountTypeRepository; 

    @Before 
    public void init() { 
    setUp("AccountType"); // Call configuration method with table name 
    accountTypeService = new AccountTypeService(accountTypeRepository)); 
    } 

    @Test 
    public void testFindByAccountTypeCodeNotIn() { 
    List<AccountType> accountTypes = accountTypeService.findByAccountTypeCodeNotIn(); 
    assertNotNull("AccountType Not null:",accountTypes); 
    assertEquals("AccountTypes Size:",3, accountTypes.size()); 
    } 

    // Database Configuration Methods 

    protected void setUp(String... setupFiles) { 
    MockitoAnnotations.initMocks(this); 
    try { 
     Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 
     Connection connection = DriverManager.getConnection("jdbc:derby:memory:testdb;create=true"); 
     for (String fileName : setupFiles) { 
      ij.runScript(connection, getClass().getResourceAsStream("/sql/" + fileName + ".sql"), "UTF-8", 
        System.out, "UTF-8"); 
     } 
    } catch (Exception e) { 

    } 
} 

public static void remove() { 
    try { 
     DriverManager.getConnection("jdbc:derby:memory:testdb;drop=true").close(); 
    } catch (SQLNonTransientConnectionException ntce) { 
    } catch (SQLException e) { 
    } 
} 
} 
0

このコード例が役に立ちますようお願いいたします。

@RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(classes=AddressBookConfiguration.class) 
    public class AddressServiceTests { 

     @Autowired 
     private AddressService addressService; 

     @Test 
     public void testService() { 
     Address address = addressService.findByLastName("Sheman"); 
     assertEquals("P", address.getFirstName()); 
     assertEquals("Sherman", address.getLastName()); 
     assertEquals("42 Wallaby Way", address.getAddressLine1()); 
     assertEquals("Sydney", address.getCity()); 
     assertEquals("New South Wales", address.getState()); 
     assertEquals("2000", address.getPostCode()); 
     } 
    } 
+0

こんにちは下回っ見つけます、助けてくれなかった... 'addressService'はnull ... Autowiredは私にとってはうまくいかなかった..何が間違っていますか? – Sergey

+0

@georges van私はIn-Memoryテストについて質問し、In-Memoryで使用されるデータベースにデータを挿入し、テストケースでそれを使用しました。 – AdamIJK

+0

@SergeyはAutowired for Serviceクラスを使用せず、サービスクラスを宣言してテストケースを実行するだけで、うまくいくと思います。 – AdamIJK

関連する問題