2016-07-22 95 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

相关问题