如果类没有setter,如何将对象自动连接到bean [英] How to autowire object to bean if class does not have setter

查看:54
本文介绍了如果类没有setter,如何将对象自动连接到bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的控制器

@Controller
public class MyController {
 @Autowire
 MyClass myClass;
 //myClass doesn't have setter and getter
 .... 
 @RequestMapping("/path")
 public String underTest(){
    myClass.makeSomething();
    return "html.jsp"
}

我想使用Mockito和模拟myClass进行模拟测试. 在测试类中,我想获取myClass:

I want make mock test using Mockito and mock myClass. In test class I want get myClass so:

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/BeanConfig.xml"); 
myClass = context.getBean("myClass ", MyClass .class);

但是我需要将这个bean自动连接到Controller来测试控制器的方法(我认为测试代码不应该影响正常代码).

But I need autowire this bean to Controller for testing controller's method(I think test code should not affect to normal code).

有没有写set方法的方法?

There are exist way to make it without writing of set method?

我想检查myClass.makeSomething()是否在underTest方法中调用一次.

I want to check that myClass.makeSomething() invokes once in method underTest.

推荐答案

我不确定我是否同意测试代码不会影响正常代码.我认为,重构/重写生产代码的一个完全正当的理由是使其更具可测试性(这可能是通过使其具有更高的模块化性来实现的,这通常是一件好事).

I'm not sure I agree with you that test code should not affect normal code. I think an entirely valid reason to refactor / rewrite production code is to make it more testable (this is probably achieved by making it more modular, which is generally a good thing anyway).

这正是批注之类的原因

@VisibleForTesting

存在.然后,您可以为MyClass创建一个程序包本地的setter,添加上面的注释(以供其他程序员和可能的代码检查工具参考),并在测试中设置该字段(应位于同一程序包中).

exist. Then you can create a package-local setter for MyClass, add the above annotation (for information to other programmers and possibly code inspection tools) and set the field in your test (which should reside in the same package).

或者,由于您使用的是Mockito,因此可以简单地使用@InjectMocks注释MyController实例,例如

Alternatively, since you are using Mockito, you could simply annotate the MyController instance with @InjectMocks, eg

@Test
public class MyControllerTest {

    @Mock
    private MyClass mockMyClass;
    @InjectMocks
    private MyController myController;

    @BeforeMethod
    public void before() {
        MockitoAnnotations.initMocks(this);
    }

    // do tests...
}

请注意,@ InjectMocks不依赖于目标字段上的任何注释(即,@ Autowired,@ Resource,@ Inject等).它只是工作. (大概对于Spring注入,您仍然需要这些注释,因此请不要删除它们!关键是您还可以将其用于未注释的字段).

Note that @InjectMocks does not depend on any annotations on the target field (i.e. @Autowired, @Resource, @Inject etc). It just works. (Presumably you will still need those annotations for Spring injection, so don't remove them! The point is you can also use it for fields that aren't annotated).

还要注意,根据所使用的Mockito版本,您可能需要在调用MockitoAnnotations.initMocks()之前在before()方法中实例化MyController

Note also that, depending on which version of Mockito you are using, you may need to instantiate the MyController in the before() method before calling MockitoAnnotations.initMocks()

这篇关于如果类没有setter,如何将对象自动连接到bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆