2016-07-22 97 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編寫單元測試用例,mockito任何人都可以幫助我嗎?

回答

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'爲空...自動佈線對我來說不起作用..我做錯了什麼? – Sergey

+0

@georges van我問In-Memory測試,在數據庫中插入數據,這些數據用於內存和測試用例。 – AdamIJK

+0

@Sergey不使用Autowired for Service類,只聲明服務類並運行測試用例,希望它能爲你工作。 – AdamIJK

相關問題