Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AOP proxy is not being executed #20

Closed
mpprdev opened this issue Apr 4, 2019 · 3 comments
Closed

AOP proxy is not being executed #20

mpprdev opened this issue Apr 4, 2019 · 3 comments

Comments

@mpprdev
Copy link

mpprdev commented Apr 4, 2019

I've a spring bean whose methods are advised with some extra logic in @aspect. When I try to mockk or spykk this bean, the extra logic is not being invoked. Is there a way around this?

@jnizet
Copy link
Member

jnizet commented Apr 4, 2019

It's hard to know what the problem can be without a reproducible test case. My guess is that you're hitting spring-projects/spring-boot#5837, which is mentioned in the README as a limitation. But it can only be a guess.

@mpprdev
Copy link
Author

mpprdev commented Apr 5, 2019

Here is a reproducible test case with the following result

org.opentest4j.AssertionFailedError: value must be 201: mocked 101 + aop 100 ==> 
Expected :201
Actual   :101
package com.example.springmockkaop

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Import
import org.springframework.stereotype.Component
import org.springframework.test.context.junit.jupiter.SpringExtension

open class BarService() {
	open fun doSomething(): Int = 100
}

open class FooService (private val barService: BarService) {

	open fun doSomething(): Int = barService.doSomething()
}

@Aspect
@Component
class FooAspect {
	@Around("target(com.example.springmockkaop.BarService) && execution(public * doSomething(..))")
	fun afterAdvice(joinPoint: ProceedingJoinPoint): Int {
		return joinPoint.proceed() as Int + 100
	}
}

@TestConfiguration
class TestConfig {

	@MockkBean
	@Autowired lateinit var barService: BarService

	@Bean
	fun fooService(barService: BarService): FooService = FooService(barService)
}

@ExtendWith(SpringExtension::class)
@Import(TestConfig::class)
@ComponentScan("com.example.springmockkaop")
class SpringmockkaopApplicationTests {

	@Autowired lateinit var fooService: FooService
	@Autowired lateinit var barService: BarService

	@Test
	fun testAOP() {

		every {
			barService.doSomething()
		} returns 101

		assertEquals(201, fooService.doSomething(), "value must be 201: mocked 101 + aop 100")
	}
}

@jnizet
Copy link
Member

jnizet commented Apr 5, 2019

Thanks.

But the same test using the standard MockBean annotation and Mockito fails in the same way.
I'd say that it is expected behavior: you're testing FooService, and mocking its dependency BarService, so it seems normal to me that the mocked bar service returns what you tell it to return: the FooService test shouldn't even have to know that there is an aspect involved.

Anyway, if it's a bug and not a feature, the bug also exists in Spring test, and SpringMockK is consistent with it.

@jnizet jnizet closed this as completed Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants