Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Nunit. Framework;
Using System. reflection;
Namespace Com. XXX. utils
{
/// <Summary>
/// Unit Test Tool
/// </Summary>
Public Static Class Unittesthelper
{
Public Static Void Assertexception < Tcallback >
( String Errormessage,
Tcallback callbackmethod,
Params Object [] Parameters)
{
Object Method = Callbackmethod As Object ;
Bool Hasexception = False ;
Try
{
(Delegate) method). dynamicinvoke (parameters );
}
Catch (Exception E)
{
Hasexception = True ;
Assert. areequal (errormessage, E. innerexception. Message );
}
If ( ! Hasexception)
{
Throw New Exception (errormessage + " Exception not found! " );
}
}
Public Static Void Setnonpublicfield ( Object OBJ, String Fieldname, Object Newvalue)
{
Try
{
Fieldinfo Field = OBJ. GetType (). getfield (fieldname,
Bindingflags. nonpublic | Bindingflags. instance );
Field. setvalue (OBJ, newvalue );
}
Catch (Exception E)
{
Throw E. innerexception;
}
}
Public Static Object Getnonpublicfield ( Object OBJ, String Fieldname)
{
Try
{
Fieldinfo Field = OBJ. GetType (). getfield (fieldname,
Bindingflags. nonpublic | Bindingflags. instance );
Return Field. getvalue (OBJ );
}
Catch (Exception E)
{
Throw E. innerexception;
}
}
Public Static Void Setnonpublicproperty ( Object OBJ, String Propertyname, Object Newvalue)
{
Try
{
Propertyinfo Property = OBJ. GetType (). getproperty (propertyname,
Bindingflags. nonpublic | Bindingflags. instance );
Property. setvalue (OBJ, newvalue, Null );
}
Catch (Exception E)
{
Throw E. innerexception;
}
}
Public Static Object Getnonpublicproperty ( Object OBJ, String Propertyname)
{
Try
{
Propertyinfo Property = OBJ. GetType (). getproperty (propertyname,
Bindingflags. nonpublic | Bindingflags. instance );
Return Property. getvalue (OBJ, Null );
}
Catch (Exception E)
{
Throw E. innerexception;
}
}
Public Static Object Callnonpublicmethod ( Object OBJ, String Methodname, Params Object [] ARGs)
{
Try
{
Methodinfo Method = OBJ. GetType (). getmethod (methodname,
Bindingflags. nonpublic | Bindingflags. instance );
Return Method. Invoke (OBJ, argS );
}
Catch (Exception E)
{
Throw E. innerexception;
}
}
}
} Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Nunit. Framework;
Using Com. XXX. utils;
Namespace Com. XXX. XXX. Test. utils
{
Public Class A
{
Private Int PRI;
Protected StringPro;
Protected StringPro {Get;Set;}
Private String Primethod ( String Hello)
{
Return Hello + " Xhan PRI " ;
}
Protected String Promethod ( String Hello)
{
Return Hello + " Xhan pro " ;
}
}
[Testfixture]
Public Class Unittesthelpertest
{
[Test]
Public Void Testsetgetnonpoublicfield ()
{
A = New A ();
Unittesthelper. setnonpublicfield (, " PRI " , 1 );
IntPRI=(Int) Unittesthelper. getnonpublicfield (,"PRI");
Assert. areequal (1, PRI );
Unittesthelper. setnonpublicfield (, " Pro " , " Hello " );
Assert. areequal ( " Hello " , Unittesthelper. getnonpublicfield (, " Pro " ). Tostring ());
}
[Test]
Public Void Testgetsetnonpublicproperty ()
{
A = New A ();
Unittesthelper. setnonpublicproperty (, " Pro " , " Xhan " );
Assert. areequal ( " Xhan " , Unittesthelper. getnonpublicproperty (, " Pro " ));
}
[Test]
Public Void Testcallnonpublicmethod ()
{
A = New A ();
String Priresult = Unittesthelper. callnonpublicmethod (, " Primethod " , " Ni Hao " ). Tostring ();
Assert. areequal ("Ni Hao xhan PRI", Priresult );
string proresult = unittesthelper. callnonpublicmethod (A, " promethod " , " Ni hao " ). tostring ();
assert. areequal ( " Ni hao xhan pro " , proresult);
}< BR >}< br>