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

Mockito cannot mock AspectJ-based @Cacheable method #24735

Closed
cdalexndr opened this issue Mar 8, 2020 · 5 comments
Closed

Mockito cannot mock AspectJ-based @Cacheable method #24735

cdalexndr opened this issue Mar 8, 2020 · 5 comments
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue

Comments

@cdalexndr
Copy link

Spring Boot 2.2.5.RELEASE

Mocking a @Cacheable method with @SpyBean results in null when calling method.
Example:

@Service
public class SomeService{
    @Cacheable("somecache")
    public String method() {
        return "not";
    }
}
@SpringBootTest
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class TestClass extends AbstractTestNGSpringContextTests {
    @SpyBean SomeService spybean;

    @Test
    public void test(){
       doReturn( "expected" ).when( spybean ).method();
       assertEquals( spybean.method(), "expected" ); //fails
    }
}

@wilkinsona
Copy link
Member

Having copied those two classes into a minimal sample application configured to use TestNG, I cannot reproduce the problem. If you would like us to spend some more time investigating, please spend some time providing a complete and minimal sample that reproduces the problem. You can share it with us by zipping it up and attaching it to this issue or by pushing it to a separate repository on GitHub.

@cdalexndr
Copy link
Author

https://github.com/cdalexndr/spring-boot-issue-20426
just run gradlew test
I forgot to mention that I'm using aspectj. It may fail only when using aspectj mode.

@cdalexndr cdalexndr changed the title @Spybean cannot mock @Cacheable method @Spybean cannot mock apsectj @Cacheable method Mar 9, 2020
@mbhave
Copy link
Contributor

mbhave commented Mar 10, 2020

Another SpyBean issue that might be related: #24724

@wilkinsona
Copy link
Member

Thanks for the sample. As @mbhave suspected, this appears to be very similar to #24724. Here's a sample that reproduces the problem without any involvement from Spring Boot:

package example;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Application.class)
public class SomeServiceTest {

    @Spy
    SomeService someService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testCacheable() {
        doReturn("yes").when(someService).cacheableMethod();
        assertThat(someService.cacheableMethod()).isEqualTo("yes");
    }

    @Test
    public void testNormal() {
        doReturn("yes").when(someService).normalMethod();
        assertThat(someService.normalMethod()).isEqualTo("yes");
    }

}

@Service
class SomeService {

    @Cacheable("someCache")
    public String cacheableMethod() {
        return "not";
    }

    public String normalMethod() {
        return "not";
    }
}

@ComponentScan
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
class Application {

    @Bean
    ConcurrentMapCacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }

}
plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.aspectj', name: 'aspectjweaver'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    implementation 'org.springframework:spring-aspects'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    File springAgent = configurations.runtimeClasspath.filter { f -> f.name.matches("aspectjweaver.*\\.jar") }.iterator().next();
    jvmArgs += ["-javaagent:" + springAgent.absolutePath]
}

@wilkinsona wilkinsona changed the title @Spybean cannot mock apsectj @Cacheable method Mockito spy does not work with @Cacheable method Mar 19, 2020
@bclozel bclozel transferred this issue from spring-projects/spring-boot Mar 19, 2020
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 19, 2020
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@snicoll snicoll changed the title @Spybean cannot mock apsectj @Cacheable method Mockito cannot mock AspectJ-based @Cacheable method Sep 25, 2023
@snicoll snicoll added in: test Issues in the test module and removed in: core Issues in core modules (aop, beans, core, context, expression) labels Sep 25, 2023
@snicoll
Copy link
Member

snicoll commented Sep 25, 2023

Closing as a duplicate of #24724. I'll drop a note there to make sure to review the reproducer above.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2023
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

6 participants