小编Rom*_*ner的帖子

如何测试@Cacheable?

我正在努力在Spring Boot Integration Test中测试@Cacheable.这是我第二天学习如何进行集成测试以及我发现使用旧版本的所有示例.我还看到了一个例子assetEquals("some value", is())但没有任何一个import语句来知道哪个依赖"是"属于哪个.测试在第二次失败

这是我的集成测试....

@RunWith(SpringRunner.class)
@DataJpaTest // used for other methods
@SpringBootTest(classes = TestApplication.class)
@SqlGroup({
        @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
                scripts = "classpath:data/Setting.sql") })
public class SettingRepositoryIT {

    @Mock
    private SettingRepository settingRepository;

    @Autowired
    private Cache applicationCache;


    @Test
    public void testCachedMethodInvocation() {
        List<Setting> firstList = new ArrayList<>();
        Setting settingOne = new Setting();
        settingOne.setKey("first");
        settingOne.setValue("method invocation");
        firstList.add(settingOne);

        List<Setting> secondList = new ArrayList<>();
        Setting settingTwo = new Setting();
        settingTwo.setKey("second");
        settingTwo.setValue("method invocation");
        secondList.add(settingTwo);

        // Set up the mock to return *different* …
Run Code Online (Sandbox Code Playgroud)

java junit integration-testing spring-test

10
推荐指数
1
解决办法
1万
查看次数

使用带有OnException定义的adviceWith进行Camel路由测试

我有一个非常简单的Camel路由定义,它只包含一些OnException谓词来处理各自的异常和一些日志语句.

from("hazelcast:seda:someQueue")
    .id("someQueueID")
    .onException(CustomException.class)
        .handled(true)
        .log(LoggingLevel.WARN, "custom exception noticed")
    .end()
    .onException(IOException.class, FileNotFoundException.class)
        .asyncDelayedRedelivery()
        .redeliveryDelay(3*1000*60) // 3 Minutes
        .maximumRedeliveries(3)
        .log(LoggingLevel.WARN, "io exception noticed")
    .end()
    .onException(Exception.class)
        .log(LoggingLevel.WARN, "general exception noticed")
    .end()

    .log("Starting route")
    .bean(TestBean.class)
    .log("Finished route");
Run Code Online (Sandbox Code Playgroud)

bean本身也很简单,它只是检查一个header参数并抛出一个适当的异常

public class TestBean
{
    @Handler
    public void checkData(@Headers final Map<String, Object> headers)
            throws CustomException, IOException, Exception
    {
        Integer testVal = (Integer)headers.get("TestValue");
        if (0 == testVal)
            throw new CustomException("CustomException");
        else if (1 == testVal)
            throw new IOException("IOException");
        else
            throw new Exception("Exception");
    }
}
Run Code Online (Sandbox Code Playgroud)

由于这个测试设置只是一个较大项目的一小部分,这样做可能听起来很愚蠢,但核心意图是在测试时修改redeliveryDelay,因为"强制"IOException不需要等待3分钟因此,为了加快单位测试,可以将重新传递延迟减少到10毫秒.

为了实现这一点,我的测试方法执行以下操作:

@ContextConfiguration(classes …
Run Code Online (Sandbox Code Playgroud)

java unit-testing apache-camel

7
推荐指数
1
解决办法
7637
查看次数

通过SpringCache缓存嵌套的可缓存操作

我被赋予了将SpringCache用于我们的一项服务以减少数据库查找次数的任务.在测试实现时,我注意到一些可缓存的操作是通过log-statements多次调用的.调查显示,如果在可缓存方法中调用可缓存操作,则根本不缓存嵌套操作.因此,稍后调用嵌套操作会导致进一步查找.

下面列出了一个描述问题的简单单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringCacheTest.Config.class} )
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class SpringCacheTest {

  private final static String CACHE_NAME = "testCache";
  private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private final static AtomicInteger methodInvocations = new AtomicInteger(0);

  public interface ICacheableService {

    String methodA(int length);
    String methodB(String name);
  }

  @Resource
  private ICacheableService cache;

  @Test
  public void testNestedCaching() {

    String name = "test";
    cache.methodB(name);
    assertThat(methodInvocations.get(), is(equalTo(2)));

    cache.methodA(name.length());
    // should only be 2 as methodA for this length was already invoked before
    assertThat(methodInvocations.get(), is(equalTo(3))); …
Run Code Online (Sandbox Code Playgroud)

java spring caching

3
推荐指数
1
解决办法
1125
查看次数