如何在dbunit数据集中插入换行符?像这样:
<user id="1"
story="first line
second line
third line"/>
Run Code Online (Sandbox Code Playgroud)
如果我以这种方式执行db 中的字段故事,则只会出现由空格分隔的情况,但我需要换行。
我在 Windows 10 上的 vagrant 设置中安装了 php 7.0.29 版本。
当我运行以下命令时
composer require --dev phpunit/phpunit
Run Code Online (Sandbox Code Playgroud)
我得到了提到的错误
Using version ^6.5 for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: don't install phpunit/phpunit 6.5.8
- Conclusion: don't install phpunit/phpunit 6.5.7
- Conclusion: don't install phpunit/phpunit 6.5.6
- Conclusion: don't install phpunit/phpunit 6.5.5
- Conclusion: don't install phpunit/phpunit 6.5.4
- Conclusion: …Run Code Online (Sandbox Code Playgroud) 使用以下查询运行测试时,HSQLDB将表别名错误地作为模式.
SELECT c.country_ml2country as CTRY_PK, c.name as CTRY_NAME,
l.name as LANGUAGE_NAME, l.code as LANGUAGE_CODE
FROM country_ml as c, language as l
WHERE c.language(+) = l.id and c.country_ml2country(+) = ?
ORDER BY l.name ASC;
Run Code Online (Sandbox Code Playgroud)
有谁之前经历过这个吗?如果"是",那么修复是什么?
请注意,如果我更改FROM country_ml as c为FROM country_ml as bob,则错误消息会相应更改为invalid schema name: BOB.
我在尝试保存实体时收到错误.我启动服务器时只会出现此错误,而不是在使用dbunit运行单元测试时.
我想保存一个协会.我的单元测试应该与手动测试时遇到的完全相同.我在关系的一端添加了一个新实体,之前没有任何关系.
我使用HSQLDB进行单元测试,Web应用程序正在使用SQL Server.
我所进行的搜索并未取得丰硕成果.对该消息的解释将证明是非常有用的.
这是测试用例(工作得很好!):
@Test
@DatabaseSetup(value="MobileWebsiteTest.saveMobilewebsiteMobilecolorswatchmapuserdefined_NewUserSwatch.xml", type=DatabaseOperation.REFRESH)
public void saveMobilewebsiteMobilecolorswatchmapuserdefined_NewUserSwatch() {
// given
Integer mobileWebsiteId = 569;
Mobilecolorswatchmapuserdefined expected = MobilecolorswatchmapuserdefinedBuilder.init().build();
// when
Mobilewebsite result = service.saveMobilewebsiteMobilecolorswatchmapuserdefined(mobileWebsiteId, expected);
// then
assertNotNull("The Mobilewebsite user defined swatch should not be null", result.getMobilecolorswatchmapuserdefined());
assertNotNull("The Mobilewebsite user defined swatch id should not be null.", result.getMobilecolorswatchmapuserdefined().getMobileColorSwatchMapUserDefinedId());
assertEquals("The result aside property should be equivalent to the expected aside property.", expected.getAside(), result.getMobilecolorswatchmapuserdefined().getAside());
assertEquals("The result SiteName property should be equivalent to the expected SiteName property.", expected.getSiteName(), …Run Code Online (Sandbox Code Playgroud) 我使用HSQLDB编写junits,我的查询是这样的:
String queryStr = "from ManualUrlBatchModel where status IN(:status) group by batchUser order by creationTime";
Query query = getSession(requestType).createQuery(queryStr);
query.setParameterList("status", status);
Run Code Online (Sandbox Code Playgroud)
我在给定状态下检索每个用户一个批次(取决于创建时间FIFO顺序).
它适用于端到端测试,但在编写junits时失败.
例外说:
Caused by: java.sql.SQLException: Not in aggregate function or group by clause: org.hsqldb.Expression@164f8d4 in statement [select manualurlb0_.manual_url_batch_id as manual1_7_, manualurlb0_.creation_time as creation2_7_, manualurlb0_.modification_time as modifica3_7_, manualurlb0_.attribute_list as attribute4_7_, manualurlb0_.batch_name as batch5_7_, manualurlb0_.batch_user as batch6_7_, manualurlb0_.input_s3_key as input7_7_, manualurlb0_.locale as locale7_, manualurlb0_.notify_when_complete as notify9_7_, manualurlb0_.output_s3_key as output10_7_, manualurlb0_.processed_url_count as processed11_7_, manualurlb0_.s3_bucket as s12_7_, manualurlb0_.status as status7_, manualurlb0_.submitted_url_count as …Run Code Online (Sandbox Code Playgroud) 这是scanario:
我正在研究一个DAO对象,它使用hibernate标准API来形成一些复杂的查询来执行数据库上的某些任务(例如,跨多个字段的关键字搜索).
我们需要对此进行单元测试,以确保生成的查询对于各种场景都是正确的.测试它的一种方法 - 可能更好 - 将通过最后检查并模拟数据库交互来测试hibernate标准是否正确创建.然而,这是不可取的,因为它首先是有点作弊(它只是复制了代码将要做的事情),而且它也不会检查标准本身是否会导致休眠到barf,或者当它进入数据库时它会导致问题.
然后,使用选项对测试数据库运行查询.但是,由于历史原因,没有静态测试数据库(例如,代码作为代码的一部分签入的代码),并且我的项目的职权范围不允许我开始创建一个,我们必须满足于对共享开发数据库,定期使用生产数据刷新.
当这些刷新发生时,测试背后的数据也会发生变化,这将使我们的单元测试变得脆弱.我们可以通过在测试中不使用确切的数字来克服它,但这并不是真正适合测试的方式.
那么问题是:人们在这样的情况下做些什么来使测试变得不那么脆弱?我想到的一个选项是运行一个执行相同查询的本机SQL(行为 - 它不必与hibernate生成的查询完全相同)来获取预期的数字,然后运行DAO版本以查看如果它匹配.这样,查询的行为总是可以在初始本机SQL中实现,并且您将始终拥有正确的数字.
关于如何管理这种情况的这个或其他想法的任何反馈将不胜感激.
一个.
更新:
关于hsqldb/h2/derby的建议,我对它们很熟悉,但该公司尚未准备好沿着这条路走下去,只在一个测试用例上零碎地做它将不合适.
关于我之前的建议,我想详细说明一下 - 考虑这种情况:
我想确保我的相对复杂的关键字搜索返回"John Smith"的2100个匹配项.
为了找到预期的数字,我会分析我的数据库,并使用SQL查询找出数字.将该查询作为测试的一部分有什么缺点,以便您始终知道您正在测试标准的行为?
基本上问题是:如果由于某种原因你没有静态数据集进行测试,你将如何以非脆弱的方式执行集成测试?
在运行PHPUnit测试时,会创建许多连接但不会关闭.我可以看到这个
mysql> show processlist;
Run Code Online (Sandbox Code Playgroud)
在我的数据库类中,我通过实现PHPUnit_Extensions_Database_TestCase#getConnection()来创建数据库连接.
我确保在拆解时连接关闭.请参阅代码段:
<?php
abstract class My_Tests_DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
{
static private $pdo = null;
private $conn = null;
/**
* @throws RuntimeException
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
final public function getConnection()
{
$iniFilePath = __DIR__ . '/../../../db-config.ini';
$iniFile = parse_ini_file($iniFilePath, true);
$dsn = "mysql:dbname=".$iniFile['phpunit']['dbname'].";host=".$iniFile['phpunit']['host'];
if ( $this->conn === null ) {
if ( self::$pdo == null ) {
self::$pdo = new \PDO($dsn, $iniFile['phpunit']['user'], $iniFile['phpunit']['password']);
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, $iniFile['phpunit']['dbname']);
}
return $this->conn;
}
protected function getSetUpOperation()
{ …Run Code Online (Sandbox Code Playgroud) 我为我的一个模块创建了一个数据库测试类.当我尝试运行测试用例时,初始夹具未设置 - 测试数据库中的所有表都是空的.这是我的getDataSet()方法:
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/dataSets/initial.xml');
}
Run Code Online (Sandbox Code Playgroud)
我发现getDataSet()方法被调用,因为当我在其中创建语法错误时,测试执行失败.但是当我在文件initial.xml中创建错误时,没有任何反应.似乎文件initial.xml根本没有被解析!
该文件的路径应该没问题,但我注意到当我输入无效路径时,没有任何事情发生.显然,方法createFlatXMLDataSet()在出错时不会抛出任何异常.所以现在我没有任何线索为什么它不起作用:(
如何使用ExpectedDataSet注释忽略某些列的比较?
dbunitAPI.我正在尝试使用Spring Test DBUnit来运行集成测试,以检查DAO的服务是否正常运行.对于两个类似的实体,我能够创建运行正常的测试,但是对于这个特定的实体,测试无法正常运行.
测试将被忽略,我将在控制台中看到的唯一例外是:
java.lang.IllegalArgumentException: Unable to load dataset from "data/offline_message.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader
Run Code Online (Sandbox Code Playgroud)
这是相关文件.XML文件:
<dataset>
<mp_account id="1" auth_hash="ted.mosby" first_name="Ted" last_name="Mosby" credential="EMAIL" transport_session="someTransportSession"/>
<mp_account id="2" auth_hash="lily.aldrin" first_name="Lily" last_name="Aldrin" credential="MEH" transport_session="someTransportSession"/>
<mp_message id="1" recipient_account_id="1" sender_account_id="2"/>
</dataset>
Run Code Online (Sandbox Code Playgroud)
失败的测试类:
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.somecompany.messaging.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.List;
@DatabaseSetup("data/message.xml")
public class MessageDaoTest extends AbstractDaoTest<Message> {
private static final Long ACCOUNT_ID = 1L;
public static final long DATE_LONG = 1431018764154L;
private static final Date LAST_UPDATE_TS …Run Code Online (Sandbox Code Playgroud)