spring 在测试类中使用appconfig的ThreadPoolTaskExecutor bean

0yg35tkg  于 2023-10-15  发布在  Spring
关注(0)|答案(1)|浏览(136)

我不能在Service Test类中使用app Config中定义的bean,尽管它在Service类中工作得很好。如何使用测试豆?我能够以一种稍微不同的方式使用ReflectionTestUtils来使用执行器,但不是像这样使用bean。
我的应用程序配置看起来像这样-

@Configuration
@EnableAsync
@EnableScheduling
public class ApplicationConfiguration {

 @Bean(name = "executor")
  public ThreadPoolTaskExecutor executor(
      @Value("${thread-pool.size}") int size,
      @Value("${thread-pool.shutdownTime}") int shutdownTime) {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(size);
    executor.setMaxPoolSize(size);
    executor.setAwaitTerminationSeconds(shutdownTime);

    executor.initialize();
    return executor;
  }
  
}

服务-

public class RandomService {

private ThreadPoolTaskExecutor executor;

public RandomService(@Qualifier("executor") ThreadPoolTaskExecutor executor) {
this.executor = executor;
}

public CompletableFuture<Void> getInfo(DataRequest request) {
return CompletableFuture.runAsync(
            () -> {
              DataResponse dataResponse =
                  getProcessedData(request);
            }, executor)
        .exceptionally(
            ex -> {
             log.error("Exception occurred :: ", ex);
            });
}

}
测试-

@RunWith(MockitoJUnitRunner.class)
public class randomServiceTest {
@InjectMocks private RandomService randomService;
private ThreadPoolTaskExecutor executor; // How to use the bean defined in appconfig with all values in Test? 

@Before
  public void setUp() throws Exception {
    randomService = new RandomService(executor);
    executor.setCorePoolSize(10); // executor is null here
  }
@Test
  public void testgetInfo() throws Exception {
    randomService.getInfo(request);
  }
}

[更新] - @Ken Chan的回答确实有帮助。我确实有一些其他的模拟,只是我只包括了前面感兴趣的代码。现在我尝试使用另一个现有的(在代码库中)TestConfiguration类,如下所示-

@Configuration
@Profile("test")
public class TestConfiguration {

@Bean(name = "executor")
    public ThreadPoolTaskExecutor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setAwaitTerminationSeconds(50);
        executor.initialize();
        return executor;
    }
}

我尝试使用如下的spring test support将此bean加载到test class中的spring上下文中,但所有mocks和test config中的executor bean都为null,无法正确注入-

@SpringBootTest
@ExtendWith(SpringExtension.class)
@Slf4j
@ContextConfiguration(classes = TestConfiguration.class)
@ActiveProfiles("test")
public class RandomServiceTest {
@InjectMocks private RandomService randomService;
@MockBean private SomeService someService; //Tried @Mock of Mockito, but it's null
@Autowired private ThreadPoolTaskExecutor executor; // null
@Before
  public void setUp() throws Exception {
    randomService = new RandomService(someService, executor);
  }
}

有谁能把问题提出来吗?

zwghvu4y

zwghvu4y1#

如果你只想在不启动spring上下文的情况下编写普通的Junit测试,你可以简单地创建一个ApplicationConfiguration并调用executor()来手动创建ThreadPoolTaskExecutor示例,如下所示:

public class randomServiceTest {

 private RandomService randomService;
 private ThreadPoolTaskExecutor executor; 

  @Before
  public void setUp() throws Exception {
     ApplicationConfiguration appConfig = new ApplicationConfiguration();
     var executor = appConfig.executor(10,10);
     randomService = new RandomService(executor);
  }

}

另外,由于您没有创建任何@Mock,因此在这里使用@InjectMocks作为没有@Mock注入是没有意义的。只是简单地把所有的东西都连接起来,以保持简单。

相关问题