Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock or stub for chained call

protected int parseExpire(CacheContext ctx) throws AttributeDefineException {     Method targetMethod = ctx.getTargetMethod();     CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);     ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class);     // check for duplicate setting     if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) {         throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr");     }     // expire time defined in @CacheEnable or @ExpireExpr     return cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE ? cacheEnable.expire() : parseExpireExpr(cacheExpire, ctx.getArgument()); } 

that is the method to test ,

Method targetMethod = ctx.getTargetMethod(); CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class); 

I have to mock three CacheContext,Method and CacheEnable. Is there any idea to make the test case much simpler?

like image 360
jilen Avatar asked Oct 28 '11 08:10

jilen


People also ask

What is stub and mock?

Stub: a dummy piece of code that lets the test run, but you don't care what happens to it. Substitutes for real working code. Mock: a dummy piece of code that you verify is called correctly as part of the test. Substitutes for real working code.

How do you mock chain methods in jest?

You can use mockFn. mockReturnThis() to do this. const client = { items: () => { return client; }, type: (name: string) => { return client; }, toObservable: () => { return client; }, subscribe: handler => { handler(); return client; } }; export { client };

What is the difference between doReturn and thenReturn?

One thing that when/thenReturn gives you, that doReturn/when doesn't, is type-checking of the value that you're returning, at compile time. However, I believe this is of almost no value - if you've got the type wrong, you'll find out as soon as you run your test. I strongly recommend only using doReturn/when .

Does mock object call real method?

Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().


1 Answers

Mockito can handle chained stubs:

Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);  // note that we're stubbing a chain of methods here: getBar().getName() when(mock.getBar().getName()).thenReturn("deep");  // note that we're chaining method calls: getBar().getName() assertEquals("deep", mock.getBar().getName()); 

AFAIK, the first method in the chain returns a mock, which is set up to return your value on the second chained method call.

Mockito's authors note that this should only be used for legacy code. A better thing to do otherwise is to push the behavior into your CacheContext and provide any information it needs to do the job itself. The amount of information you're pulling from CacheContext suggests that your class has feature envy.

like image 196
Lunivore Avatar answered Sep 24 '22 21:09

Lunivore