2

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?

AdamIJK
  • 615
  • 2
  • 12
  • 22

2 Answers2

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());
      }
    }
krynio
  • 2,442
  • 26
  • 30
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • Hi, didn't help... `addressService ` is null... Autowired didn't work for me.. What do I do wrong? – Capacytron Apr 02 '17 at 18:17
  • @georges van I asked about the In-Memory test, inserting data in database which is used In-memory and test cases with that. – AdamIJK Apr 16 '17 at 17:07
  • @Sergey do not use Autowired for Service class, just declare the service class and run the test cases, hope it will work for you. – AdamIJK Apr 19 '17 at 04:31
1

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) {
    }
}
}
AdamIJK
  • 615
  • 2
  • 12
  • 22