Class method expectation not working - ocmock

I tried to use class method stubs and expectations with new OCMock 3.0 syntax but I did not succeed. :(
I would like to verify that a class method is called. In order to understand why my expectation is not fulfilled, I simplified the example to this:
- (void)test_should_call_XXX {
id classMock = OCMClassMock([NSString class]);
[NSString string];
OCMVerify([classMock string]);
}
I do not understand why this lines of code do not work:
OCMockObject(NSString): Method string was not invoked
I tried OCMVerify(ClassMethod([classMock string]));with the same result.
Regards,
Quentin

Class methods on NSString cannot be verified. Please have a look at the limitations section 10.5 in the documentation: http://ocmock.org/reference/#limitations

Related

KafkaListener annotation at class level with errorhandler property ignored

When using the kafkalistener annotation at class level and the provided errorhandler property is ignored. When method is annotated with kafkalistner and the provided errorhandler is working. Is it expected behavior?
This is really bug. The piece of code:
String errorHandlerBeanName = resolveExpressionAsString(kafkaListener.errorHandler(), "errorHandler");
if (StringUtils.hasText(errorHandlerBeanName)) {
endpoint.setErrorHandler(this.beanFactory.getBean(errorHandlerBeanName, KafkaListenerErrorHandler.class));
}
Is missed in the:
private void processMultiMethodListeners(Collection<KafkaListener> classLevelListeners, List<Method> multiMethods,
Object bean, String beanName) {
Unfortunately I don't see a simple way to workaround this. Please, consider to have a single #KafkaListener method with an Object as payload and manual type routing in that method to others.
Feel free to raise a GitHub issue on the matter!

Kotlin reflection returns incorrect type

I have an activity that implements a listener with a generic type that requires me to implement the onResponse function.
The listener is defined in the following format:
public interface ListenerClass<T> {
onResponse(T type)
}
And my activity implements it using T == ModelClass.
class MyActivity : ListenerClass<ModelClass> {
(...)
override fun onResponse(response: ModelClass) {
(...)
}
}
The listener is part of an SDK so I cannot change any code in there. They use a piece of reflection to get the type of my 'ModelClass' using the following code:
listener.getClass().getDeclaredMethods()[onResponseIndex].getParameterTypes()[0]
The result of this code should be Class<ModelClass> but I get Class<Object>. I found out that when I call the code above on a Java class, I get the correct result, but my activity is written in Kotlin.
1) How can I get the parameter type of a kotlin function using reflection?
2) If I have an answer to question 1 I still won't be able to change the SDK's code. Is there anything I can write in my Kotlin code that makes the piece of code above find the correct parameter class type?

PHPUnit Doctrine Repository magic methods

I need some help to fully test my class that depends on Doctrine.
I have one method in my class that uses that magic method findOneBy... from the Doctrine EntityRepository, as showed below:
But when I run my test I always have this warning:
How can I mock that call? Below I put how EntityManager magic __call method supposed to work:
You can mock magic method __call which is invoked when invoking unacessible methods.
$mock
->expects($this->once())
->method('__call')
->with(
$this->equalTo('findOneByIdShopUrl'), //
$this->equalTo(['5'])
)
->willReturn(['shop' => 'shop info']); // return shop object
Check also http://php.net/manual/en/language.oop5.overloading.php#object.call
The other option is to use setMethods()
$this->getMockBuilder('ShopRepository')->setMethods(['findOneByShopId'])->getMock();
and then the rest logic with methods like will, with etc.
I think that you can replace the magic method findOneByXXXX('identifier') with the real method findOneBy(array('XXXX' => 'identifier')).
That is to say:
$this->shopUrlRepository->findOneByIdShopUrl($this->shop->getId());
To:
$this->shopUrlRepository->findOneBy(array('IdShopUrl' => $this->shop->getId()));
That is a real method. Doctrine documentation
I hope this can help you.

asmock Previous method requires a return value or an exception to throw

Trying to get my head around asmock to implement some unit testing in my project. I want to test my MainMediator and since there are objects that get created in my MainMediator onRegister call, I'm thinking that I should mock those objects.
Hopefully that's correct to begin with!
I have something like this
[Rule] public var includeMocks : IncludeMocksRule = new IncludeMocksRule([
IEventDispatcher, IMyService
]);
[Before]
public function setUp():void {
mockRepository = new MockRepository();
mainView = new MainView();
mainMediator = new MainMediator();
dispatcher = IEventDispatcher(mockRepository.createStub(IEventDispatcher, StubOptions.NONE));
myService = IMyService(mockRepository.createStub(IMyService, StubOptions.NONE));
mockRepository.stubEvents(dispatcher);
SetupResult.forCall(chatService.clientID)
.returnValue("");
mockRepository.replayAll();
mainMediator.eventDispatcher = dispatcher;
myService.eventDispatcher = dispatcher;
mainMediator.service = myService;
....
mainMediator.onRegister();
}
When I step through the test and stop at mockRepository.stubEvents(dispatcher). I can see errors in the myService class
Error: Previous method IMyService/clientID/get(); requires a return value or an exception to throw. clientID just happens to be my first property hence why it's being picked on.
I thought either that StubOptions.NONE would mean that no properties get stubbed or that my SetupResult.forCall(myService.clientID) would fix it but none did.
Answering to the question in the comment re: the eventDispatcher, I have:
MyService extends ServiceBase implements IMyService
where ServiceBase extends Actor
I found that I need the following in IMyService to get access to the eventDispatcher.
function get eventDispatcher():IEventDispatcher;
function set eventDispatcher(dispatcher:IEventDispatcher):void;
Not too sure if that is correct. Bit confused now.
Can someone please tell me where I'm going wrong?
Thanks!
This is a common problem when mocking concrete classes, rather than interfaces: if the constructor calls another method (or property getter), it will return null because it hasn't been mocked yet.
There's not really anyway to workaround it, except to abstract your class through an interface and mock that.

How does the #ModelAttribute annotation work? Why can't I got the value?

Here is my code:
public ModelAndView login(#ModelAttribute("testVO") TestVO testVO){
//test the VO work theory
//testVO = new TestVO();
testVO.setTestStr("this is my test!");
return "index/index";
}
When I used the new to create a object for the testVO.
I can not get the value in my jsp page.
If I used set method, it works.
So, I think:
The object testVo has been create by the IOC container, so the JSP get the reference from the container, not the one I create by my self.
Am I right?
Thanks in advanced.
You guessed it right. Below text is from spring docs:
An #ModelAttribute on a method argument indicates the argument should be
retrieved from the model. If not present in the model, the argument should be
instantiated first and then added to the model.
If you want to create it yourself, you explicity need to add it to the model (as below) so that can be used in your jsp
public String login(Model model){
TestVO testVO = new TestVO();
testVO.setTestStr("this is my test!");
model.addAttribute("testVO", testVO);
return "index/index";
}
Man, here is the best explanation of #ModelAttribute annotation:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods
Read at least two chapters. This will give you a theoretical knowledge.
A practical side of question you can find on my blog:
http://fruzenshtein.com/spring-mvc-form-handling/
I hope it will be helpful for you

Resources