Spring数据jpa存储库内存测试用例

时间:2016-07-22 11:52:55

标签: spring junit mockito spring-data-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可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

希望此代码示例有所帮助。

    @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());
      }
    }

答案 1 :(得分:1)

我们可以通过创建与Derby的内存数据库连接来实现内存测试案例,我已经完成了德比,在我的代码下面找到

测试类

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) {
    }
}
}