在测试期间注入@Autowired 私有字段 [英] Injecting @Autowired private field during testing

查看:39
本文介绍了在测试期间注入@Autowired 私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件设置,它本质上是一个应用程序的启动器.它是这样配置的:

I have a component setup that is essentially a launcher for an application. It is configured like so:

@Component
public class MyLauncher {
    @Autowired
    MyService myService;

    //other methods
}

MyService 使用 @Service Spring 注释进行注释,并自动连接到我的启动器类中,没有任何问题.

MyService is annotated with the @Service Spring annotation and is autowired into my launcher class without any issues.

我想为 MyLauncher 编写一些 jUnit 测试用例,为此我创建了一个这样的类:

I would like to write some jUnit test cases for MyLauncher, to do so I started a class like this:

public class MyLauncherTest
    private MyLauncher myLauncher = new MyLauncher();

    @Test
    public void someTest() {

    }
}

我可以为 MyService 创建一个 Mock 对象并将其注入到我的测试类中的 myLauncher 中吗?我目前在 myLauncher 中没有 getter 或 setter,因为 Spring 正在处理自动装配.如果可能,我不想添加 getter 和 setter.我可以告诉测试用例使用 @Before init 方法将模拟对象注入到自动装配的变量中吗?

Can I create a Mock object for MyService and inject it into myLauncher in my test class? I currently don't have a getter or setter in myLauncher as Spring is handling the autowiring. If possible, I'd like to not have to add getters and setters. Can I tell the test case to inject a mock object into the autowired variable using an @Before init method?

如果我完全错了,请随意说出来.我还是新手.我的主要目标是拥有一些 Java 代码或注释,将模拟对象放入 @Autowired 变量中,而我不必编写 setter 方法或使用 applicationContext-test.xml 文件.我更愿意在 .java 文件中维护测试用例的所有内容,而不必为我的测试维护单独的应用程序内容.

If I'm going about this completely wrong, feel free to say that. I'm still new to this. My main goal is to just have some Java code or annotation that puts a mock object in that @Autowired variable without me having to write a setter method or having to use an applicationContext-test.xml file. I would much rather maintain everything for the test cases in the .java file instead of having to maintain a separate application content just for my tests.

我希望将 Mockito 用于模拟对象.过去,我通过使用 org.mockito.Mockito 并使用 Mockito.mock(MyClass.class) 创建对象来完成此操作.

I am hoping to use Mockito for the mock objects. In the past I have done this by using org.mockito.Mockito and creating my objects with Mockito.mock(MyClass.class).

推荐答案

您绝对可以在测试中在 MyLauncher 上注入模拟.我相信如果您展示您使用的模拟框架,某人会很快提供答案.使用 mockito,我会考虑使用 @RunWith(MockitoJUnitRunner.class) 并为 myLauncher 使用注释.它看起来像下面的内容.

You can absolutely inject mocks on MyLauncher in your test. I am sure if you show what mocking framework you are using someone would be quick to provide an answer. With mockito I would look into using @RunWith(MockitoJUnitRunner.class) and using annotations for myLauncher. It would look something like what is below.

@RunWith(MockitoJUnitRunner.class)
public class MyLauncherTest
    @InjectMocks
    private MyLauncher myLauncher = new MyLauncher();

    @Mock
    private MyService myService;

    @Test
    public void someTest() {

    }
}

这篇关于在测试期间注入@Autowired 私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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