使用entityManager.createQuery(sql.toString());查询为空。使用dbUnit

时间:2019-01-27 07:23:29

标签: hibernate spring-boot dbunit

我是dbUnit的新手,正在为hibernetJpa编写测试用例,我尝试了很多方法并对其进行了很多搜索,但是没有运气。我正在附上我的代码,请帮助我。

test-accountSampleDB.xml

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <TEST_DB 
        TOK_ID="123"
        DATA_SENT_DATE="2018/11/01"
    />
</dataset>

DAO类

   @Repository
    @Transactional
    public class TestDaoJpaImpl {

        @PersistenceContext(unitName="testPersistance")
        private EntityManager entityManager;

        public TestDaoJpaImpl() {
            super();
        }

        public Date getDataSendDate(String tockVal) {

            StringBuilder sql= new StringBuilder();
            sql.append("select t.urlSendDate from myTable t where t.tokVal= :tock");
            Query query= entityManager.createQuery(sql.toString());
            query.setParameter("tock", tockVal);
            return (Date) query.getSingleResult();
        }
    }

测试类

@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(classes= {DBUnitHibernateBeanConfig.class}, loader=AnnotationConfigCOntextLoader.class)
@TestExecutionListeners({
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class
})
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)
@DbUnitConfiguration(dataSetLoader=XmlDataSetLoader.class)
public class TestDaoJpaTest {

    TestDaoJpaImpl accountDaoJpa;

    @Mocked // mockit.Mocked
    private EntityManager entityManager;

    @Before
    @DatabaseSetup(type=DatabaseOperation.CLEAN_INSERT, value= {"test-accountSamplrDB.xml"})
    public void startUp() {
        accountDaoJpa= new TestDaoJpaImpl();
        Deencapsulation.setField(accountDaoJpa,"entityManager", entityManager);
    }

    @Test
    public void test_doSomeWork() {
        Date urlSendDate=accountDaoJpa.getDataSendDate("123456");
        Date compareDate= new Date(2019/04/01);
        assertEquals(urlSendDate,compareDate);
    }
}

POM.xml

<dependencies>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.8</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.github.springtestdbunit</groupId>
        <artifactId>spring-test-dbunit</artifactId>
        <version>1.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.5.1</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <artifactId>junit</artifactId>
                <groupId>junit</groupId>
            </exclusion>
        </exclusions>
    </dependency>

在TestDaoJpaImpl.java类中,“查询查询= entityManager.createQuery(sql.toString());”查询即将为空。我不知道为什么我也尝试模拟查询,但无法获取查询的值。 预先感谢您的帮助。

0 个答案:

没有答案