В настоящее время я пытаюсь создать модульные тесты, используя MSTest.exe для многопоточности.Когда я запускаю тесты, вот ошибки, которые я получаю:
A first chance exception of type 'System.NullReferenceException' occurred in ApplicationManagement.exe
The thread 'Agent: adapter run thread for test 'UpdateDirectoryExceptionTest' with id 'a78d3e8e-e859-43aa-87aa-cf006f736dee'' (0x1150) has exited with code 0 (0x0).
The thread '<No Name>' (0x1bec) has exited with code 0 (0x0).
E, 6788, 86, 2011/10/20, 14:02:36.771, WKSTVMC0006\QTAgent32.exe, Unhandled Exception Caught, reporting through Watson: System.NullReferenceException: Object reference not set to an instance of an object.
at CommonObjects4.clsThread.StartThreadMethod.Execute() in D:\DevProjects\CommonObjects4\classes\clsThread.cs:line 23
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The program '[6788] QTAgent32.exe: Managed (v4.0.30319)' has exited with code -2 (0xfffffffe).
The program '[1120] ApplicationManagement.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
Если я сам запускаю UpdateDirectoryExceptionTest в Visual Studio, он проходит без проблем, но когда я запускаю весь набор модулейтесты, я получаю вышеуказанную ошибку.Вот функция, которую я недавно начал тестировать, которая, как мне кажется, связана с ошибкой:
public Thread StartClassThread(ref Thread ThreadObject, ThreadMethod MethodObject)
{
System.Threading.Thread startClassThreadReturn = null;
try
{
if (ThreadObject == null && MethodObject == null)
{
throw new ApplicationException("Cannot have both ThreadObject and MethodObject as null parameters in call to StartClassThread.");
}
StartThreadMethod objMethod = new StartThreadMethod();
objMethod.objThreadMethod = MethodObject;
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState != System.Threading.ThreadState.Stopped)
{
// Do nothing
}
}
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState == System.Threading.ThreadState.Stopped
| ThreadObject.ThreadState == System.Threading.ThreadState.Aborted
| ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
{
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
}
else
{
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
return ThreadObject;
}
catch (Exception excException)
{
ZEGApp.clsMain.objApplicationAudit.AuditMethodError(excException, System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace);
}
finally
{
// Do nothing
}
return startClassThreadReturn;
}
Вот вывод, который я получаю из CruiseControl.NET, в отличие от тестирования в Visual Studio:
<message>Passed UnitTests.clsThreadTest.StartClassThreadTest2</message>
<message>Process 'QTAgent32' [PID 3932] has finished profiling.</message>
<message>Process 'QTAgent32' [PID 5388] has begun profiling.</message>
<message>Error UnitTests.clsUltraCalendarTest.clsUltraCalendarConstructorTest</message>
У кого-нибудь есть идеи относительно того, как я могу устранить эти ошибки?TIA.
ОБНОВЛЕНИЕ : Вот полный исходный код для clsThread.cs (обратите внимание, что строка 23 - это objThreadMethod ();):
namespace CommonObjects4
{
public class clsThread
{
#region '" Sub Class "'
public delegate void ThreadMethod();
private class StartThreadMethod
{
public ThreadMethod objThreadMethod;
public void Execute()
{
objThreadMethod(); // this is line 23
}
}
#endregion
#region '" Enumerator Declaration "'
#endregion
#region '" Variable Declaration "'
#endregion
#region '" Property Declaration "'
#endregion
#region '" Function Declaration "'
public Thread StartClassThread(Thread ThreadObject, ThreadMethod MethodObject)
{
System.Threading.Thread startClassThreadReturn = null;
try
{
if (ThreadObject == null && MethodObject == null)
{
throw new ApplicationException("Cannot have both ThreadObject and MethodObject as null parameters in call to StartClassThread.");
}
StartThreadMethod objMethod = new StartThreadMethod();
objMethod.objThreadMethod = MethodObject;
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState != System.Threading.ThreadState.Stopped)
{
// Do nothing
}
}
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState == System.Threading.ThreadState.Stopped | ThreadObject.ThreadState == System.Threading.ThreadState.Aborted )
{
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
}
else
{
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
return ThreadObject;
}
catch (Exception excException)
{
ZEGApp.clsMain.objApplicationAudit.AuditMethodError(excException, System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace);
}
finally
{
// Do nothing
}
return startClassThreadReturn;
}
#endregion
}
}