Having this interface:
public interface ICandyManager
{
IReadOnlyList<string> GetCandies(in ReadOnlySpan<byte> bytes);
}
When I try mock interface with nsubstitute:
ICandyManager nb = Substitute.For<ICandyManager>();
I get following error:
Message: System.TypeLoadException : Signature of the body and declaration in a method implementation do not match. Type: 'Castle.Proxies.ICandyManager'. Assembly: 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
What is a workaround for this? Obviously I can initialize real type, but I want to be able to mock it.
I think this is thrown due to a version of Castle.Core being used that does not support reference semantics for value types.
At present there are two ways I can think of to fix it:
1) Upgrade NSubstitute to 4.0.0 Release Candidate 1 or later. This uses Castle.Core 4.3.1 (or later) and works correctly with the interface in your question.
2) Keep using NSubstitute 3.1.0, but add a reference to a more recent Castle.Core NuGet package in your test project. I tested this with NSubstitute 3.1.0 and Castle.Core 4.3.1 and my sample test started running correctly.
Related
I'm trying to call a WebService from Grails using CXF plugin version: 1.1.3
Have generated Java classes for the WSDL via the command:
WsdlToJava "--wsdl=src/java/wsdls/jdeRteListenerService.wsdl --mark -p mypackage.pms.OsrJdeRteSvc -client -impl"
When I went to run the webservice call I would get:
Class not known to this context for class: mypackage.pms.OsrJdeRteSvc.ListAllEventsRequest.
Following this stackoverflow article: JAXB Exception: Class not known to this context
I added to the Port class:
#XmlSeeAlso({mypackage.pms.OsrJdeRteSvc.impl.ObjectFactory.class, mypackage.pms.OsrJdeRteSvc.ListAllEventsRequest.class})
Now I get the error:
Unable to find a JAXB implementation to delegate.
JAXB Source has a comment:
if(!modified) {
// if the class list doesn't contain any of our classes,
// this ContextFactory shouldn't have been called in the first place
// if we simply continue, we'll just end up with the infinite recursion.
// the only case that I can think of where this could happen is
// when the user puts additional classes into the JAXB-generated
// package and pass them to JAXBContext.newInstance().
// Under normal use, this shouldn't happen.
// anyway, bail out now.
// if you hit this problem and wondering how to get around the problem,
// subscribe and send a note to users#jaxb.dev.java.net (http://jaxb.dev.java.net/)
throw new JAXBException("Unable to find a JAXB implementation to delegate");
}
https://svn.java.net/svn/jaxb~version2/tags/jaxb-2_2_3u1/xjc/src/com/sun/tools/xjc/runtime/JAXBContextFactory.java
I've subscribed to the list but not yet been approved.
Anyway, why am I getting: Unable to find JAXB implementation to delegate?
When mocking an object with PHPUnit, how do I access properties of the object which are normally accessed via overloading, i.e. via __get()?
For example, in the code below I am testing a Post object. Each Post has an author, which of type Role. Each Role has a Signature property.
$author = $this->getMockBuilder('App_Model_Domain_Role')
->disableOriginalConstructor()
->getMock();
$author->expects($this->any())
->method('__get')
->will($this->returnValue('authorname'));
As you can see I mock the Role object, then configure it to return a string ('authorname') when __get() is called. The Post object which I am testing refers to $this->author->signature. I am expecting it to return 'authorname', but instead the test errors out saying that $signature is an undefined property.
I tried configuring the mock as above but without the method() call (thinking that the expects() and will() calls would then apply to all the mock's methods) but still no success.
Any ideas?
Also, if you know of a good tutorial on PHPUnit mocks I'd be keen to see it - the manual seems to assume prior testing knowledge in this particular area.
I tried the same code as you and it is working for me. My PHPUnit version is 3.7.12. Which version are you using? If you are running a older version you could try upgrading.
public function testMockingMagicGetter()
{
$myMock = $this->getMockBuilder('MyClass')
->disableOriginalConstructor()
->getMock();
$myMock
->expects($this->any())
->method('__get')
->will($this->returnValue('authorname'));
var_dump($myMock->signature);
var_dump($myMock->dummy);
var_dump($myMock->someprop);
}
Results in:
string(10) "authorname"
string(10) "authorname"
string(10) "authorname"
I find it very odd that with such excellent Grails integration, Idea does not recognize standard JUnit assertion methods in Grails unit tests. I created a brand new project and made one domain class with corresponding test to make sure it wasn't something weird with my larger project. Even if I add a #Test annotation, the IDE does not see any assertion methods
#TestFor(SomeDomain)
class SomeDomainTests {
#Test //thought adding this, not needed for Grails tests, would help but it doesn't
void testSomething() {
assertEquals("something", 1, 1); //test runs fine, but IDE thinks this method and any similar ones don't exist
}
}
I have created an issue in IntelliJ bugtracker: http://youtrack.jetbrains.com/issue/IDEA-82790. It will be fixed in IDEA 11.1.0
As workaround you can add "import static org.junit.Assert.*" to imports.
Note: using "assert 1 == 1 : 'message'" is preferable than "assertEquals('message', 1, 1)" in groovy code.
Idea has problems if you use 'def' to define a variable (so it's type is not known) and then you try to pass it to a Java method which is strongly typed. Because it can't infer the type.
So it will give a message with words to the effect of "there is no method assertEquals() that takes arguments with type String, null, null".
I wouldn't expect this message in the example you give (because you are using ints directly, not a dynamically-typed variable) but I thought you might have missed it when trying to create a simple example code snippet for the question.
With the #TestFor annotation an AST will add methods to you test class and IDEA does not catch these methods.
You have two options:
Make the test class extends GrailsUnitTestCase.
Add dynamic method to your test class.
In our unit tests where it first load db setting from a singleton class, we have:
IKernel kernel = new StandardKernel(new OurInfrastructureNinjectModule());
_myService = kernel.Get<MyService>(); // To inject a concrete to myService
It runs fine in our mvc application, however, exception threw when it is called by unit tests.
Note: I work at Typemock
Due to changes in security in .NET 4, there was a bug in Typemock Isolator where code running from assemblies marked with AllowPartiallyTrustedCallers (APTCA) or SecurityTransparent attributes would crash with this exception. Ninject, seems like it, is marked with such an attribute.
We had fixed this issue in the latest version, so please download it at http://www.typemock.com.
If your issue persists in the latest version (6.0.9 currently), please contact us via support at typemock.com
I'm trying to write some unit tests for a service in my Grails app. The service, 'MyService', uses a class located in $APP-ROOT/src/groovy/ called 'MyHelperClass'.
In the unit test for MyService, I try to create a mock for MyHelperClass like so:
def myHelperClassMock = mockFor(MyHelperClass)
def myService = new MyService()
myService.myHelperClass = myHelperClassMock.createMock()
This gives the error:
Error casting map to com.mycompany.myproject.MyHelperClass, Reason: Could not find matching constructor for: com.mycompany.myproject.MyHelperClass()
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to com.mycompany.myproject.MyHelperClass, Reason: Could not find matching constructor for: com.mycompany.myproject.MyHelperClass()
at grails.test.GrailsMock.createMock(GrailsMock.groovy:91)
at grails.test.GrailsMock$createMock.call(Unknown Source)
at com.mycompany.myproject.MyServiceTests.testSomething(MyServiceTests.groovy:17)
This seems to happen only for classes in src/, and appears to work fine for classes in grails-app/services for example. Any idea how I can get it to see the classes in src/?
Thanks!
The code doesn't specify it, but createMock appears to require that the class being mocked have a default (no arguments) constructor.
The normal Grails artifacts under /grails-app all have these, whereas some other class under /src may not.
Adding a default constructor fixed the error when I ran into this.
Also see https://groups.google.com/forum/?fromgroups=#!topic/groovymn/u2Ng_RM224A for a related discussion of this.
This answer should give you the info you need.
Mocking out constructors in Grails