Are there any MySQL packages in memory / bullying for unit testing in Spring?

In the past, I used Fongo to write unit / integration unit tests around Mongo calls, and this is really nice. Fongo stores all the data in memory without a database, which is exactly what you would like to use for unit test.

I am wondering if there are any packages to provide the same concept of MySQL emulation? I use Spring and the JdbcTemplate class to execute queries; what I hope for is that I can log in, and any JdbcTemplate calls will, in fact, be emulated.

Do such packages exist? Or are there other methods for this?

+4
source share
2 answers

HyperSQL . 100% MySQL, .

HyperSQL . . doc.

A mem: mem:. mem: - . mem: . "mymemdb":

Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");

, JDBCTemplate, spring xml.

+3

Spring 4, . DAO. myBatis. HSQLDB DAO .

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
@ContextConfiguration("classpath:testApplicationContext.xml")
@SqlGroup({
  @Sql(scripts = "/com/acme/restangle/dao/test-person-data.sql",
  executionPhase = BEFORE_TEST_METHOD),
  @Sql(
    scripts = "/com/acme/restangle/dao/reset-person-data.sql",
    executionPhase = AFTER_TEST_METHOD)
})
public class PersonDaoImplTest
{
  @Autowired
  private PersonDao dao;

  @Autowired
  private SqlSessionTemplate sqlSessionTemplate;

  @Autowired
  private JdbcTemplate jdbcTemplate;

  @Before
  public void setUp() throws Exception
  {
    sqlSessionTemplate.getConfiguration().setCacheEnabled(false);
    sqlSessionTemplate.getConfiguration().setLocalCacheScope(LocalCacheScope.STATEMENT);
    assertFalse("expected myBatis cache to be disabled", sqlSessionTemplate.getConfiguration().isCacheEnabled());
  }

  @Test
  public void testInsert() throws Exception
  {
    assertEquals(1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "PERSON"));

    assertNull(dao.insert(null));

    Person person = new Person();
    person.setFirstName("firstName");
    person.setMiddleName("middleName");
    person.setLastName("lastName");
    person.setGender(Gender.MALE);
    person.setEthnicity(Ethnicity.ASIAN);

    Person insertedPerson = dao.insert(person);
    assertNotNull(insertedPerson);
    assertEquals(2, insertedPerson.getId());
    assertEquals(person.getFirstName(), insertedPerson.getFirstName());
    assertEquals(person.getMiddleName(), insertedPerson.getMiddleName());
    assertEquals(person.getLastName(), insertedPerson.getLastName());
    assertEquals(person.getGender(), insertedPerson.getGender());
    assertEquals(person.getEthnicity(), insertedPerson.getEthnicity());
    assertEquals(person.isDeceased(), insertedPerson.isDeceased());
  }

( ..) . . . .

, reset :

delete from PERSON;
ALTER TABLE PERSON ALTER COLUMN ID RESTART WITH 1;

Spring. , DDL :

  <context:annotation-config/>
  <context:component-scan base-package="com.acme.restangle" />

  <jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:restangle-schema.sql"/>
  </jdbc:embedded-database>

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
  </bean>

  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" primary="true">
    <constructor-arg index="0" ref="sqlSessionFactory" />
  </bean>

  <!-- scan for mappers and let them be autowired -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.acme.restangle.persistence" />
  </bean>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
+2

All Articles