I have a project using .Net 4.0, VS 2010 and Spring 1.3. Before and after each of my Integration tests run, I want to be able setup and clean up s开发者_运维百科ome data for the test. I was thinking of using custom attributes to do this.
[TestClass]
public class UnitTest1
{
[TestMethod]
[PreTestSqlExecute(SqlScript="SetUpDataForMethod1.sql")]
public virtual void TestMethod1()
{
.
To do this, I have created the custom attribute and the Around Method Interceptor
public class PreTestSqlExecuteAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
.
Basically, the problem now is how to get the Test framework in Microsoft to use Spring to create the "UnitTest1" class rather than creating a concrete class directly. For example, I can use a dummy console app with appropriate configuration to do something like this
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
var ut = (UnitTest1)ctx["mySqlTest"];
ut.TestMethod1();
This works fine. But when I run the tests directly using visual studio and clicking "Run All Tests In Solution" for example, it does not call the interceptor and execute the before and after code, just goes directly to the test method.
I have tried using AttributeAutoProxyCreator in the config as shown below
<object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
<property name="AttributeTypes" value="SpringTests.ClassInstanceAttribute"/>
<!-- Interceptor names can be either of the type IAdvice, IAdvisor, or IAdvisors -->
<property name="InterceptorNames" value="aroundAdvisor"/>
</object>
with the appropriate attribute on the class
[TestClass]
[ClassInstance]
public class UnitTest1
{
or inheriting from the AbstractDependencyInjectionSpringContextTests
public class UnitTest1 : AbstractDependencyInjectionSpringContextTests
But none of these seems to work. My method interceptor is not being called. So how do I get the Microsoft test framework in Visual Studio to either use spring context to create the Test Classes. Or alternately, get Spring to intercept the concrete constructions of these test classes and inject the Proxy class instead.
Thanks in advance for the help.
Found my own solution, as detailed here - http://www.chaitanyaonline.net/2011/09/25/improving-integration-tests-in-net-by-using-attributes-to-execute-sql-scripts/
Basically I inherit my unit tests from "ContextBoundObjects", this lets me inject my own code and do AOP like stuff. I created my own custom attributes so that I can run the pre and post processing Sql scripts.
精彩评论