1. <tfoot id='WEKjS'></tfoot>
      <legend id='WEKjS'><style id='WEKjS'><dir id='WEKjS'><q id='WEKjS'></q></dir></style></legend>

        <bdo id='WEKjS'></bdo><ul id='WEKjS'></ul>

      <small id='WEKjS'></small><noframes id='WEKjS'>

      <i id='WEKjS'><tr id='WEKjS'><dt id='WEKjS'><q id='WEKjS'><span id='WEKjS'><b id='WEKjS'><form id='WEKjS'><ins id='WEKjS'></ins><ul id='WEKjS'></ul><sub id='WEKjS'></sub></form><legend id='WEKjS'></legend><bdo id='WEKjS'><pre id='WEKjS'><center id='WEKjS'></center></pre></bdo></b><th id='WEKjS'></th></span></q></dt></tr></i><div id='WEKjS'><tfoot id='WEKjS'></tfoot><dl id='WEKjS'><fieldset id='WEKjS'></fieldset></dl></div>

      模拟中的弹簧值注入

      时间:2023-09-25
      <tfoot id='Nx9yE'></tfoot>
        <tbody id='Nx9yE'></tbody>
      • <bdo id='Nx9yE'></bdo><ul id='Nx9yE'></ul>

          <small id='Nx9yE'></small><noframes id='Nx9yE'>

          • <i id='Nx9yE'><tr id='Nx9yE'><dt id='Nx9yE'><q id='Nx9yE'><span id='Nx9yE'><b id='Nx9yE'><form id='Nx9yE'><ins id='Nx9yE'></ins><ul id='Nx9yE'></ul><sub id='Nx9yE'></sub></form><legend id='Nx9yE'></legend><bdo id='Nx9yE'><pre id='Nx9yE'><center id='Nx9yE'></center></pre></bdo></b><th id='Nx9yE'></th></span></q></dt></tr></i><div id='Nx9yE'><tfoot id='Nx9yE'></tfoot><dl id='Nx9yE'><fieldset id='Nx9yE'></fieldset></dl></div>
              <legend id='Nx9yE'><style id='Nx9yE'><dir id='Nx9yE'><q id='Nx9yE'></q></dir></style></legend>

              1. 本文介绍了模拟中的弹簧值注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试为以下方法编写测试类

                I'm trying to write test class for the following method

                public class CustomServiceImpl implements CustomService {
                    @Value("#{myProp['custom.url']}")
                    private String url;
                    @Autowire
                    private DataService dataService;
                

                我在类中的一种方法中使用注入的 url 值.为了测试这一点,我编写了一个 junit 类

                I'm using the injected url value in one of the methods in the class. To test this i've written a junit class

                @RunWith(MockitoJUnitRunner.class)
                @ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
                public CustomServiceTest{
                    private CustomService customService;
                    @Mock
                    private DataService dataService;
                    @Before
                    public void setup() {
                        customService = new CustomServiceImpl();
                        Setter.set(customService, "dataService", dataService);
                    }    
                    ...
                }
                
                public class Setter {
                    public static void set(Object obj, String fieldName, Object value) throws Exception {
                        Field field = obj.getClass().getDeclaredField(fieldName);
                        field.setAccessible(true);
                        field.set(obj, value);
                    }
                }
                

                在 applicationContext-test.xml 中,我正在使用

                In applicationContext-test.xml I'm loading the property file using

                    <util:properties id="myProp" location="myProp.properties"/>
                

                但是在运行测试时,url 值没有加载到 CustomService 中.我想知道是否有办法完成这项工作.

                But the url value is not getting loaded in the CustomService on running the test. I was wondering if there is anyway to get this done.

                谢谢

                推荐答案

                您可以自动装配到 mutator(setter)中,而不仅仅是注释私有字段.然后你也可以从你的测试类中使用那个设置器.无需公开,包私有即可,因为 Spring 仍然可以访问它,但否则只有您的测试可以进入那里(或同一包中的其他代码).

                You can autowire into a mutator (setter), rather than just annotating the private field. Then you can use that setter from your test class as well. No need to make it public, package private will do as Spring can still access it, but otherwise only your test can get in there (or other code in the same package).

                @Value("#{myProp['custom.url']}")
                String setUrl( final String url ) {
                    this.url  = url;
                }
                

                我不喜欢仅仅为了测试而以不同的方式自动装配(与我的代码库相比),但是从测试中更改被测类的替代方法简直是邪恶的.

                I'm not a fan of autowiring differently (compared to my codebase) just for testing, but the alternative of changing the class under test, from the test, is simply unholy.

                这篇关于模拟中的弹簧值注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何模拟影响对象的无效返回方法 下一篇:Mockito 如何捕获传递给注入的模拟对象方法的参数

                相关文章

                最新文章

                <legend id='KnXuR'><style id='KnXuR'><dir id='KnXuR'><q id='KnXuR'></q></dir></style></legend>

                <tfoot id='KnXuR'></tfoot>

                    <small id='KnXuR'></small><noframes id='KnXuR'>

                    <i id='KnXuR'><tr id='KnXuR'><dt id='KnXuR'><q id='KnXuR'><span id='KnXuR'><b id='KnXuR'><form id='KnXuR'><ins id='KnXuR'></ins><ul id='KnXuR'></ul><sub id='KnXuR'></sub></form><legend id='KnXuR'></legend><bdo id='KnXuR'><pre id='KnXuR'><center id='KnXuR'></center></pre></bdo></b><th id='KnXuR'></th></span></q></dt></tr></i><div id='KnXuR'><tfoot id='KnXuR'></tfoot><dl id='KnXuR'><fieldset id='KnXuR'></fieldset></dl></div>
                      <bdo id='KnXuR'></bdo><ul id='KnXuR'></ul>