用于Spring Cache Manager的Junit

dev*_*per 2 java junit spring mockito spring-cache

我在应用程序中使用Spring缓存层,在编写单元测试以使用Mockito测试Spring缓存层时遇到了一个问题。

请针对我的问题参考以下代码:

服务层:

    public CustomerServiceImpl implements CustomerService {
    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private CustomerRepository customerRepository;//Repository is simple JPA repository interface which contains findByCustomerName()

    @Override
    @CachePut(value = "#customer", key = "#customer.customerName")
    public Customer insertOrUpdate(Customer customer) {
        return customerRepository.save(customer);
    }

    @Cacheable(value="customersCache", key = "#customerName")
    public Customer findByCustomerName(String customerName) {
        Customer customer = customerRepository.findByCustomerName(customerName);
        return customer;
    }
}
Run Code Online (Sandbox Code Playgroud)

用于服务层的JUnit测试代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest(CustomerServiceImplTest.class)
public class CustomerServiceImplTest {

    @Spy
    CacheManager cacheManager = new ConcurrentMapCacheManager("customersCache");

    @Mock
    CustomerRepository CustomerRepository;

    @InjectMocks
    CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();

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

    @Test
    public void testCacheForFindByCustomerName() {
        Customer customer1 = new Customer();
        customer1.setId("1");
        customer1.setName("John");
        Customer customer2 = new Customer();
        customer2.setId("2");
        customer2.setName("Sara");

        //this should save to cache   
        Mockito.when(CustomerRepository.save(customer1))
        .thenReturn(customer1);
        customerServiceImpl.insertOrUpdate(customer1);

        //Now it should retreive from cache, but not able to
      Mockito.when(CustomerRepository.findByCustomerName(Mockito.any(String.class)))
                .thenReturn(customer1, customer2);

        Customer result = customerServiceImpl.findByCustomerName("John");
        assertThat(result, is(customer1));

        result = customerServiceImpl.findByCustomerName("John");
        assertThat(result, is(customer1));
    }
}
Run Code Online (Sandbox Code Playgroud)

例外:

我收到一个“ java.lang.AssertionError:”,因为缓存层不起作用,并且调用已传递到存储库对象(两次),该对象返回了上面的“ customer2”模拟对象,即,通过传递相同的键两次调用了存储库方法服务层。

另外,请注意,我正在使用“ Mockito”框架进行测试。

我试图用Google搜索关于Spring缓存的单元测试,并且还引用了下面的URL,该URL几乎使用相同的概念,但不适用于我上面的代码。

如何在Spring数据存储库上测试Spring的声明式缓存支持?

您能帮忙解决以上例外情况吗?

Sea*_*oyd 5

Spring Cache Manager依靠Spring管理应用程序。您无法使用来获得,PowerMockRunner需要使用SpringJUnit4Runner。您仍然可以通过编程方式使用PowerMock或Mockito,但不能作为Runner使用。

通常,您会将单元测试转换为Spring风格的集成测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringTest {

    @Configuration
    @EnableCaching
    static class SpringConfig{
        @Bean
        public CustomerService customerService(){
            return new CustomerServiceImpl(customerRepository());
        }
        @Bean
        public CustomerRepository customerRepository(){
            return Mockito.mock(CustomerRepository.class);
        }
    }

    @Autowired
    CustomerService customerService; // this will contain a proper managed cache

    @Autowired
    CustomerRepository customerRepository; // this is a mockito mock you can fine-tune
}
Run Code Online (Sandbox Code Playgroud)