我正在为某些现有功能创建一些单元测试。对JPA实体上的DBUnit Blob列进行编码
我正在使用带有嵌入式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>
我的弹簧试验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());
}
该测试基本上模拟了一个字符串的职位。我只需调用String.getBytes()方法来获取Blob数据并保存它。 在实际应用程序中,当我检索Blob数据并从中实例化String时,String正好表示我在UI上发布的内容,但是当使用DBUnit在数据集中提供字节时,断言失败。请参见下文。
org.junit.ComparisonFailure:预期:< [/日志:信息 “这是我的配置文件”]>却被:< [Ѯ7n9^ X㝴5N㝴ñ﮹Μ _〜뾟n}ѭѭMñޞ獴ÿ NTN = 5N} N} MN ^띴﮵nεmmߝN×N个ѭ〜v59Nx^69M뎶랜Ο毫 덴 Nt n= 5 n} N } N x N ]> at org.junit.Assert.assertEquals(Assert.java :115) at org.junit.Assert.assertEquals(Assert.java:144) at za.co.wifire.admin.api.controller.gate way.GatewayTemplateControllerTest.testSaveEditedTemplate(GatewayTemplateControllerTest.java:133)
我认为这是由于编码差...
那么为什么不出示代码,因为现在没有什么可以评论的。或者一个问题 –
已经根据请求添加了更多信息的代码。 –
实体如何?你知道,这个Blob的东西。实体的坚持?被调用来保存BLOB的SQL。 –