在我的项目中,我编写了一个存储库类,我需要编写内存测试类。 我的存储库代码如下。
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可以帮助我吗?
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?
最满意答案
希望这段代码示例能有所帮助。
@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()); } }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) { } } }Spring数据jpa存储库内存测试用例(Spring data jpa repository In-memory test case)在我的项目中,我编写了一个存储库类,我需要编写内存测试类。 我的存储库代码如下。
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可以帮助我吗?
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?
最满意答案
希望这段代码示例能有所帮助。
@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()); } }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) { } } }
发布评论