Bug 17836 - Using Moq with Xamarin Android in Visual Studio throws exception TypeLoadException: Could not load type 'Castle.Core.Logging.TraceLogger'
Summary: Using Moq with Xamarin Android in Visual Studio throws exception TypeLoadExce...
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.10.0.x
Hardware: PC Windows
: Low normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-02-17 20:56 UTC by Jon Goldberger [MSFT]
Modified: 2014-07-30 12:18 UTC (History)
11 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test project (259.84 KB, application/zip)
2014-02-17 20:56 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 17836 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Comment 1 Jon Goldberger [MSFT] 2014-02-17 20:57:29 UTC
From desk case:
I'm trying to use Moq (http://www.moqthis.com/) in the unit testing of our project. When I run this code....

_mockContext = new Mock<Android.Content.Context>();

try
{
var obj = _mockContext.Object;
obj = _mockContext.Object;
}
catch (Exception ex)
{
System.Diagnostics.Debugger.Break();
}

It stops in the catch with this error:

+ ex {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Java.Lang.NoClassDefFoundError: Exception of type 'Java.Lang.NoClassDefFoundError' was thrown.
at Android.Runtime.JNIEnv.FindClass (System.String classname) [0x00087] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:378
at Android.Runtime.JNIEnv.FindClass (System.Type type) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:336
--- End of managed exception stack trace ---
java.lang.NoClassDefFoundError: ContextProxyc31db5492e3e4a408cbf58bef8636cf6
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "ContextProxyc31db5492e3e4a408cbf58bef8636cf6" on path: DexPathList[[zip file "/data/app/andrunit.andrunit-1.apk"],nativeLibraryDirectories=[/data/app-lib/andrunit.andrunit-1, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
... 11 more

--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy (System.Type targetType, System.Type[] interfaces, Castle.DynamicProxy.ProxyGenerationOptions options, System.Object[] constructorArgs, Castle.Core.Interceptor.IInterceptor[] interceptors) [0x00000] in <filename unknown>:0
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy (System.Type targetType, System.Type[] interfaces, Castle.DynamicProxy.ProxyGenerationOptions options, Castle.Core.Interceptor.IInterceptor[] interceptors) [0x00000] in <filename unknown>:0
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy (System.Type targetType, System.Type[] interfaces, Castle.Core.Interceptor.IInterceptor[] interceptors) [0x00000] in <filename unknown>:0
at Moq.Mock`1[Android.Content.Context].<InitializeInstance>b__26 () [0x00009] in D:\Code\moq\trunk\Source\Mock.Generic.cs:754
at Moq.PexProtector.Invoke (System.Action action) [0x00000] in D:\Code\moq\trunk\Source\Mock.Generic.cs:883
at Moq.Mock`1[Android.Content.Context].InitializeInstance () [0x00007] in D:\Code\moq\trunk\Source\Mock.Generic.cs:155
at Moq.Mock`1[Android.Content.Context].get_Object () [0x00008] in D:\Code\moq\trunk\Source\Mock.Generic.cs:147
at Ticketmaster.JACS.UnitTests.Tests.BatteryTest.Setup () [0x0000d] in c:\_jac_scanner2\Common\UnitTests\Tests\HardwareService\BatteryTest.cs:24 } System.Reflection.TargetInvocationException


Do you have any ideas for me??
Comment 2 Jon Goldberger [MSFT] 2014-02-17 21:00:40 UTC
After I suggested using the Moq fork on Xamarin's github, client responded with:

"With your version of Moq I’m getting this error…

{System.TypeInitializationException: An exception was thrown by the type initializer for Moq.Mock`1 ---> System.TypeInitializationException: An exception was thrown by the type initializer for Moq.Proxy.CastleProxyFactory ---> System.TypeLoadException: Could not load type 'Castle.Core.Logging.TraceLogger' from assembly 'Moq, Version=4.2.1308.2321, Culture=neutral, PublicKeyToken=69f491c39445e920'.
at Castle.DynamicProxy.ProxyGenerator..ctor () [0x00000] in <filename unknown>:0
at Moq.Proxy.CastleProxyFactory.CreateProxyGenerator () [0x00000] in <filename unknown>:0
at Moq.Proxy.CastleProxyFactory..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Moq.Mock`1[Android.Content.Context]..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Ticketmaster.JACS.UnitTests.Tests.BatteryTest.Setup () [0x00002] in c:\_jac_scanner2\Common\UnitTests\Tests\HardwareService\BatteryTest.cs:20 } System.TypeInitializationException"

This is the same error I was getting, almost. On my system, I got:

Could not load type 'Castle.Core.Logging.TraceLogger' from assembly 'Castle.Core ...'

instead of 

Could not load type 'Castle.Core.Logging.TraceLogger' from assembly 'Moq ...'
Comment 5 Udham Singh 2014-02-25 11:46:48 UTC
I have checked this issue and able to reproduce this.
I have implemented code mentioned in comment 1 and when debug the application i am getting exception: https://gist.github.com/saurabh360/fe23cdec1d9908caf76d

Refer screen cast: http://screencast.com/t/rxANoBxH

Environment Info:
Windows 7
VS 2012
XA 4.10.01073

Please let me know if i am missing any thing.
Comment 6 exvance 2014-07-25 13:21:50 UTC
Why was the priority of this lowered?
Comment 7 Daniel Cazzulino 2014-07-30 12:16:54 UTC
This is not related to the VS extension. Moving to the right group.