当前位置: 首页 > 面试题库 >

JUnit测试Spring @Async void服务方法

姬熙云
2023-03-14
问题内容

我有一个春季服务:

@Service
@Transactional
public class SomeService {

    @Async
    public void asyncMethod(Foo foo) {
        // processing takes significant time
    }
}

我为此进行了集成测试SomeService

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
public class SomeServiceIntTest {

    @Inject
    private SomeService someService;

        @Test
        public void testAsyncMethod() {

            Foo testData = prepareTestData();

            someService.asyncMethod(testData);

            verifyResults();
        }

        // verifyResult() with assertions, etc.
}

这是问题所在:

  • 正如SomeService.asyncMethod(..)@Async和注释的
  • 因为SpringJUnit4ClassRunner坚持@Async语义

testAsyncMethod线程将呼叫分叉someService.asyncMethod(testData)到自己的工作线程,然后直接继续执行verifyResults(),以前的工作线程完成其工作可能之前。

如何someService.asyncMethod(testData)在验证结果之前等待的完成?注意,如何使用Spring4和批注编写单元测试以验证异步行为_ 的解决方案?不要在这里申请,作为someService.asyncMethod(testData)回报void,而不是Future<?>


问题答案:

为了@Async遵守语义,某些活动@Configuration类将具有@EnableAsync注释,例如

@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {

  //

}

为了解决我的问题,我引入了一个新的Spring配置文件non-async

如果non-async配置文件 激活,AsyncConfiguration则使用:

@Configuration
@EnableAsync
@EnableScheduling
@Profile("!non-async")
public class AsyncConfiguration implements AsyncConfigurer {

  // this configuration will be active as long as profile "non-async" is not (!) active

}

如果非异步轮廓 活动的,则NonAsyncConfiguration使用:

@Configuration
// notice the missing @EnableAsync annotation
@EnableScheduling
@Profile("non-async")
public class NonAsyncConfiguration {

  // this configuration will be active as long as profile "non-async" is active

}

现在,在有问题的JUnit测试类中,我显式激活“非异步”概要文件,以相互排除异步行为:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
@ActiveProfiles(profiles = "non-async")
public class SomeServiceIntTest {

    @Inject
    private SomeService someService;

        @Test
        public void testAsyncMethod() {

            Foo testData = prepareTestData();

            someService.asyncMethod(testData);

            verifyResults();
        }

        // verifyResult() with assertions, etc.
}


 类似资料:
  • 我正在尝试从spring启动应用程序中的服务执行JUnit4测试,并且我一直使用init获取entityManagerFactory。 我还希望使用我的应用程序连接。属性文件,但它希望使用嵌入式hsqldb进行连接。 有人能给我指出正确的方向吗? 以下是相关代码: 应用特性: 主要内容: 实体: DAO/存储库: 服务: 测试: 本应包括以下内容: 堆叠:

  • 我有一个应用程序,公开Websocket/SockJS/Stomp服务器endpoint,并想运行一个JUnit测试,运行客户端(JavaSTOMP客户端,也来自Spring)对它,以测试发送功能。 我有一个测试,如 在这里,我从 https://github.com/jhalterman/concurrentunit 中使用了Water,其效果基本上是将测试的主线程延迟到辅助线程调用reachi

  • 我需要jUnit测试的帮助,我的结果是json文件,我需要测试该文件的长度,问题是在jsonPath函数中没有这样的方法。 这里是我的测试方法,预期长度是7。如有任何帮助,我将不胜感激,谢谢

  • > 解析某些文件的服务 管理文件系统的ServiceB 我想测试ControllerClass,特别是:

  • 问题内容: 我有一个充满void方法的Java类,我想进行一些单元测试以获得最大的代码覆盖率。 例如我有这种方法: 它的名字不好用是因为我翻译了代码以更好地理解。每种方法均会验证参数是否在某种程度上有效并且编写正确。 范例: 我的单元测试很好地涵盖了小的方法,因为我要求它们检查ErrorFile是否包含错误消息,但是我看不到如何测试方法checkIfValidElements,它什么也不返回或什么

  • 在我的业务逻辑中,我没有任何异常场景,不知道如何在sprint引导服务类中测试catch块。大多数场景都是在控制器级别处理的。 但是对于声纳覆盖,我需要在我的服务中覆盖捕获块。 我的示例代码如下 我的业务逻辑是否应该有任何失败场景要测试,或者我们可以选择模拟自定义虚拟异常以进行测试。任何人都请就此提出建议。 参考文献 如何断言JUnit 4测试中抛出了某个异常? 如何在我的代码中测试try-cat