S2Container.NET version 1.1 and higher includes testing framework to make testing much easier.
The framework extends MbUnit and has the following features:
If there is a field in a Test class that is not neither static, readonly, nor final and there exists a component with a name that equals the field name without the underscore (prefix or suffix), then that field and the component will be binded.
If there is a component (reference datatype or DateTime) in a container that can be substituted, that component is retrieved from the S2Container and set.
When the test method finishes, values that were set are reset to null (Nothing in VB).
If SetUpXxx() method and TearDownXxx() method is defined, they will be called after SetUp() method and before TearDown() method. Initialization and clean up steps for each test may be written in these methods.
If TxEnumerator(Seasar.Extension.Unit.Tx) is set to Rollback(Enumerator) in the S2 attribute in test method, transaction will marked to begin just before the test method and rolled back after test method finishes execution. This will allow tests to be repeated with the same set of database data.
New DataSet may be retrieved by a primary key using Reload(DataSet) to reload data. Result from an execution may be compared with the expected result entered in Excel.
Example:
DataSet expected = ReadXls("expectedResult.xls");
S2Assert.AreEqual(expected, Reload(expected);
Expected result of datatype DataSet may be compared to IList of IDictionary and IDictionary, object, IList of object by using the S2Assert.AreEqual() function.
Test class is created by extending Seasar.Extension.Unit.S2TestCase. This class should define TestFixture attribute(MbUnit.Framework.TestFixtureAttribute).
Test method should define Test attribute(MbUnit.Framework.TestAttribute) as well as S2 attribute(Seasar.Extension.Unit.S2Attribute). The test class will then create a S2Container.
<TestFixture()> _
PublicClass HogeTest
Inherits S2TestCase
<Test(), S2()> _
Publicsub TestHogeHoge()
' test codesEndSubEnd Class
S2TestClass has Register(), GetComponent(), and Include() methods that may be used with S2Container.NET.
C#
publicvoid TestHogeHoge()
{
// read dicon file
Include("Hoge.dicon");
// Register components into a S2Container
Register(typeof(Hashtable));
// Retrieve components from a S2Container
Hashtable table = (Hashtable) GetComponent(typeof(Hashtable));
}
VB.NET
<Test(), S2()> _
Publicsub TestHogeHoge()
' read dicon file
Include("Hoge.dicon")
' Register components into a S2Container
Register(typeof(Hashtable))
' Retrieve components from a S2ContainerDim table As Hashtable = CType(GetComponent(GetType(Hashtable)), Hashtable)
' when there is no explicit cast, may be defined as below' Dim table As Hashtable = GetComponent(GetType(Hashtable))End Sub
If the PATH of the dicon file is the same as the namespace of the test class, path in the namespace may be omittted. When PATH of the dicon file is specified in the file system, path will become relative path from the assembly. (However, process such as the buildin event, to output to the same folder as the assembly is necessary.)
if Test.dicon is in the namespace Foo.Fuga, it is possible to omit dicon file path on classes that are in Foo.Fuga namespace.
If there is a field in a Test class that is not neither static, readonly, nor final and there exists a component with a name that equals the field name without the underscore (prefix or suffix), then that field and the component will be binded.
If there is a component (reference datatype or DateTime) in a container that can be substituted, that component is retrieved from the S2Container and set.
If SetUpXxx() method and TearDownXxx() method is defined, they will be called after SetUp() method and before TearDown() method. Initialization and clean up steps for each test may be written in these methods.
C#
[TestFixture]
publicclass HogeTest : S2TestCase
{
publicvoid SetUpHogeHoge()
{
// initialization process
}
[Test, S2]
publicvoid TestHogeHoge()
{
// test codes
}
publicvoid TearDownHogeHoge()
{
// final processing
}
}
VB.NET
<TestFixture()> _
PublicClass HogeTest
Inherits S2TestCase
PublicSub SetUpHogeHoge()
' initialization processEndSub
<Test(), S2()> _
PublicSub TestHogeHoge()
' test codesEndSubPublicSub TearDownHogeHoge()
' final processingEndSubEnd Class
If TxEnumerator(Seasar.Extension.Unit.Tx) is set to Rollback(Enumerator) in the S2 attribute in test method, transaction will marked to begin just before the test method and rolled back after test method finishes execution. This will allow tests to be repeated with the same set of database data.
If Commit(Enumerator) in TxEnumerator is specified beside Rollback, transaction will be committed. If NotSupported(Enumerator) is specified, transaction will not begin. If TxEnumerator is not assigned to a S2 attribute, it will default to NotSupported(Enumerator).
Furthermore, to use transaction, a dicon file with TransactionContext and DataSource definition is necessary.
In this example, employee table is queried using employee number as a key. When employee table is queried for employee number 9900, that row joined with corresponding row from the deptartment table is returned.
The test case for this example should consist of expected employee table row and department table row. This expected data is specified in an Excel sheet - sheet name should correspond to the database table name, first row in the sheet should contain column names, and expected row data should be specified from the second row. The data may be manually entered into the Excel sheets or generated from the database table.
To run this example, first Setup a database to create a demostration database table used in this example. Excel sheets may then be filled with data from the database table using tool Db2ExcelClient in Seasar.Examples.
SqlReader fills DataSet from the database tables. The first argument to AddTable is the tableName(sheetName). The second argment should contain the condition.
XlsWriter outputs DataSet to Excel. File path relative to the output folder is specified in the constructor.
Data from a database table may be written out to an Excel sheet by executing Read() method in SQLReader class retrieved from IS2Container and then executing Write() method in XlsWrite class retrieved from IS2Container.
Check to make sure the data was created by executing Seasar.Examples, selecting S2Container reference from the tree, selecting "Test on Select Query(1)". There should be GetEmployeePrepare.xls in the folder Visual Studio output path + specified path (example: bin\Debug\Seasar.Examples\Reference\S2Unit). Double click on GetEmployeePrepare.xls to open this Excel sheet. In emp sheet, change EMPNO to 9900, ENAME to SCOTT2, DEPTNO to 99. In the dept sheet, change DEPTNO to 99, DNAME to RESEARCH2. Save and close the sheets.
Next, it is necessary to create data to verify data. Db2ExcelClient2 (available in Seasar.Examples) may be used to create this data.
using System.Data;
using MbUnit.Core.Cons;
using MbUnit.Framework;
using Seasar.Extension.DataSets.Impl;
using Seasar.Extension.Unit;
namespace Seasar.Examples.Reference.S2Unit
{
[TestFixture]
publicclass EmployeeDaoTest : S2TestCase
{
private IEmployeeDao dao_ = null;
publicvoid SetUpGetEmployee()
{
Include("Seasar.Examples/Reference/S2Unit/EmployeeDao.dicon");
}
[Test, S2(Tx.Rollback)]
publicvoid GetEmployee()
{
ReadXlsWriteDb("Seasar.Examples/Reference/S2Unit/GetEmployeePrepare.xls");
Employee emp = dao_.GetEmployee(9900);
DataSet expected = ReadXls("Seasar.Examples/Reference/S2Unit/GetEmployeeExpected.xls");
S2Assert.AreEqual(expected, emp, "1");
}
publicvoid Main()
{
using (MainClass mc = new MainClass())
{
mc.Main(newstring[] { "Seasar.Examples.exe" });
}
}
}
}
Excecute Seasar.Examples. To start testing, select S2Container Reference from the tree. Select Test on Select query(3).
Test may be executed from the console by executing MbUnit, but it is easier to use MbUnit.GUI.exe and TestDriven.NET.
SetUpGetEmployee() as app.dicon.
S2Assert.AreEqual() method compares the DataSet with IList of IDictionary and IDictionary, object, and IList of object.
ReadXlsWriteDb() method and ReadXlsAllReplaceDb() methods are used to write test data to the database.
If Excel file is in the same namespace as the test class, namespace may be omitted from the path. (However, it is necessary to use build event to output to the same folder as the assembly.)
To rollback after each test, execute ReadXlsWriteDb() andReadXlsAllReplaceDb() at the beginning of the test method.
Execution result are compared with data in an Excel sheet.
By passing data retrieved from Dao and verification data in Excel sheet retrieved from ReadXls() method to the S2Assert.AreEqual() method, data retrieved from Dao is converted to DataSet and is used in comparison with data from the Excel sheet.
S2Unit.NET reads sheet in alphabetical order and not in the order they are defined in the Excel sheets as in S2Unit.Java.
If there is an error due to foreign key constraint, prefix sheet name with "#". For example, to insert data from the emp table into the third sheet, name the Excel sheet "#3 emp".