Spring data jpa repository In-memory test case

2019-04-03 00:42发布

问题:

In My project I wrote a repository class for that i need to write in-memory test class. My Repository code is as follows.

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

}

for this I need to write unit test case using junit, mockito can anyone help me?

回答1:

Hope this code example will help.

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


回答2:

We can achive the In-Memory Test case by creating the In-Memory Database Connection with Derby, I have done with derby, find below my code

Test Class

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