Java 使用@Autowired fields测试Spring控制器时,字段返回null,但获取控制器

Java 使用@Autowired fields测试Spring控制器时,字段返回null,但获取控制器,java,dependency-injection,spring-test-mvc,Java,Dependency Injection,Spring Test Mvc,嗨,我有一个控制器,我正在尝试测试,没有模仿 @Controller public class MyController { @Autowired private Type1 service1; @Autowired private Type2 service2; .... } 测试等级: public class testController { @Autowired private MyController controller

嗨,我有一个控制器,我正在尝试测试,没有模仿

@Controller
public class MyController
{
    @Autowired
    private Type1 service1;

    @Autowired
    private Type2 service2;

    ....
} 
测试等级:

public class testController
{
    @Autowired
    private MyController controller;

    @Before
    public void setUp()
    {
        //setupStuff

    }

    @Test
    public void testControllerMethod()
    {
        //Test method
    }

}
当通过测试进行调试时,我得到控制器的值,但自动连线服务1和2的值为空

xml文件确实包含

如果删除它,则会导致创建bean时出错。即使我删除了其中一个服务基础包

是否有任何特定于测试的内容需要添加到配置中

类型1:

public interface Type1
{
    String method1();
    String method2();
}
自动连线的Type1实现类是:

@Service
public class Type1Class implements Type1
{
    @Autowired
    private Type3 service3;

    //Methods implementatoin of Type1
}



Type2 is similar to this.

给我们看看你的配置。对于要进行
@Autowired
的实例,需要声明或扫描bean。显示
Type1
Type2
的源代码。我已将基本包添加到上下文:component scanbase package=“controller的包,Type1的包,…等”。我需要添加到这个问题中吗?我将添加源代码的模拟作为问题的编辑。您是否用
@Service
注释了
Type1
Type2