You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单元测试Bean注入时依赖不满足问题排查

Fixing NoSuchBeanDefinitionException in Spring Unit Test with Mocked Dependency

Let's walk through the issues in your code and fix them one by one to get your test working correctly.

Key Problems in Your Original Code

  1. Syntax Error in ComponentScan:
    Your @ComponentScan annotation has a syntax mistake — basePackageClasses{com.tasks.ITest.class} is missing the equals sign and proper parentheses. This prevents Spring from scanning your ISync component, so it never gets registered as a bean in the test context.

  2. Incorrect Context Configuration:
    You've set @ContextConfiguration(classes = ITest.class), but ITest is your test class, not a proper Spring configuration class. While it contains an inner configuration class, Spring might not correctly pick it up unless you explicitly reference the inner configuration.

  3. Missing Injection of the Class Under Test:
    Your test class doesn't inject the ISync bean, so you can't actually test its someMethod() behavior. Even if ISync were registered, you wouldn't have an instance to work with.

Corrected Test Class Code

package com.tasks;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@RunWith(SpringJUnit4ClassRunner.class)
// Explicitly reference the inner configuration class
@ContextConfiguration(classes = ITest.ITestConfiguration.class)
@ActiveProfiles("test")
public class ITest {

    @Autowired
    private IAccessor iAccessor1; // Mocked instance

    @Autowired
    private ISync iSync; // Class under test, injected with the mocked IAccessor

    @Test
    public void testRun() {
        // Execute the method we want to test
        iSync.someMethod();
        
        // Verify that the mocked method was called (optional but recommended)
        verify(iAccessor1).someOtherMethod();
    }

    @Configuration
    @Profile("test")
    // Fixed syntax for ComponentScan
    @ComponentScan(basePackageClasses = {com.tasks.ITest.class})
    public static class ITestConfiguration {

        @Bean
        @Primary // Ensures this mock takes precedence over any other IAccessor beans
        public IAccessor iDataAccessor() {
            return mock(IAccessor.class);
        }
    }
}

What We Fixed & Why

  • ComponentScan Syntax: The corrected @ComponentScan(basePackageClasses = {com.tasks.ITest.class}) tells Spring to scan all components in the com.tasks package (including your ISync bean).
  • Context Configuration: By pointing directly to ITestConfiguration.class, we ensure Spring loads the exact configuration we need for the test, including the mocked IAccessor bean.
  • Injected ISync: Now we have an instance of the class under test, and Spring automatically injects the mocked IAccessor into it (thanks to @Primary ensuring the mock is used over any real implementations).
  • Added Mock Verification: I included a verify() call to show how you can confirm the mocked method was invoked, which is a common practice in unit testing with Mockito.

Additional Checks

If you still run into issues, double-check:

  • Your ISync class has the @Component annotation and lives in the com.tasks package (or a subpackage scanned by ComponentScan).
  • The ITestConfiguration is a static inner class — this is required for Spring to load it without instantiating the test class first.
  • Your ActiveProfiles("test") is correctly applied, so only the test-specific configuration is loaded.

内容的提问来源于stack exchange,提问作者Bhavya Arora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:29:01