我正在为一些现有的功能创建一些单元测试。
我正在使用带有嵌入式H2数据库的DBUnit来测试一些JPA实体。我们的生产环境使用SQL Server。
我遇到的问题是,我需要对其中一个实体上的Blob列执行一些操作,但似乎如果我将Blob数据的内容从SQL Server的某一行复制到我的dbunit xml数据集中,当我从字节实例化一个字符串时,它并不表示我所期望的文本。
实体的代码片段:
@Entity
@Table(name = "mn_gateway_template")
public class GatewayTemplate implements Serializable {
@Lob
@Column(name = "config_file_bytes", length = 500000)
private byte[] configFileBytes;
}为了保存字节,我这样做:
GatewayTemplate template = entityManager.find(GatewayTemplate.class, 1l);
byte[] bytes = postedStringContent.getBytes();
template.setConfigFileBytes(bytes);
entityManager.persist(template);我的数据集:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<mn_gateway_template id="4" disabled="0" description="A config file" config_file_bytes="00101111 01101001 01101110 01110100" />
</dataset>我的spring-test-mvc测试:
@Test
@WithMockUser
public void testSaveEditedTemplate() throws Exception {
Account account = new Account();
account.setId(1l);
mvc.perform(
post("/admin/gateway/config/template/save")
.sessionAttr("account", account)
.param("configTemplateFileName", "testConfig.txt")
.param("configFileText", "/log :info \"This is my config file \"")
.param("configurationOwnerAccount","1")
.param("model", "1")
.param("termsAccepted", "true")
.param("masterTemplateId", "1")
);
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
GatewayTemplate editedTemplate = entityManager.find(GatewayTemplate.class, 1l );
Assert.assertEquals("/log :info \"This is my config file \"", editedTemplate.getConfigFileText());
}这个测试基本上是模拟字符串的post。我只需调用String.getBytes()方法来获取Blob数据并保存它。在实际的应用程序中,当我检索Blob数据并从中实例化一个字符串时,该字符串准确地表示了我在UI上发布的内容,但是当字节在dataset中使用DBUnit提供时,下面的断言fails.See。
日志:预期:但是was:<Ѯ7�n9�^x㝴�5�n㝴�n�﮹�Μ��_�~�뾟�n�����������}�ѭ�ѭ��M���N���ޞ��獴��y���Nt�n=��5�n}�N�}���M��N��^�띴﮵�norg.junit.Assert.assertEquals(Assert.java:115)上的��ε�m��m�ߝ��N��x�N������ѭ�~v�5�9�Nx�^6��9�M�뎶�������랜�Ο����������m���M��N�덴�n�뾽����Nt�n=��5�n}�N�}�N��x�N�>在za.co.wifire.admin.api.controller.gateway.GatewayTemplateControllerTest.testSaveEditedTemplate(GatewayTemplateControllerTest.java:133)的org.junit.Assert.assertEquals(Assert.java:144)
我假设这是由于编码差异...
发布于 2016-01-06 19:58:53
事实证明,DBUnit只支持Base64编码字符串形式的二进制数据。
我认为我的问题更多的是在设计而不是其他方面。blob列实际上应该是一个简单的字符串,但它以前是一个blob,以适应文件上传,并且不能更改,因为它当前包含我们需要的数据。
https://stackoverflow.com/questions/34627049
复制相似问题