模拟中的弹簧值注入

roh*_*hit 20 java spring properties spring-mvc mockito

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

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

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

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在applicationContext-test.xml中我正在使用加载属性文件

    <util:properties id="myProp" location="myProp.properties"/>
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Rob*_*tto 47

import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public CustomServiceTest{

@InjectMock
private CustomServiceImpl customService;

@Mock
private DataService dataService;

@Before
public void setup() {
    ReflectionTestUtils.setField(customService, "url", "http://someurl");
}    
...
}
Run Code Online (Sandbox Code Playgroud)

  • 你能用几句话解释一下你的解决方案吗? (2认同)

Bri*_*ice 12

我同意@skaffman的评论.

除了你的测试使用MockitoJUnitRunner,因此它不会寻找任何Spring的东西,这唯一的目的是初始化Mockito模拟.该ContextConfiguration是不够的连线事情春天.从技术上讲,如果你想要与春天相关的东西,你可以使用以下的跑步者:SpringJUnit4ClassRunner.

另外,在编写单元测试时,您可能需要重新考虑使用弹簧.在单元测试中使用弹簧接线是错误的.但是,如果您正在编写集成测试,那么为什么在那里使用Mockito,它没有意义(正如skaffman所说)!

编辑:现在在你的代码中你直接设置CustomerServiceImpl你的前一个块,这也没有意义.Spring并没有参与其中!

@Before
public void setup() {
    customService = new CustomServiceImpl();
    Setter.set(customService, "dataService", dataService);
}
Run Code Online (Sandbox Code Playgroud)

编辑2:如果你想要写一个单元测试的CustomerServiceImpl,则避免春天的东西,直接注入属性的值.您也可以使用Mockito将DataServicemock straigth注入测试实例.

@RunWith(MockitoJUnitRunner.class)
public CustomServiceImplTest{
    @InjectMocks private CustomServiceImpl customService;
    @Mock private DataService dataService;

    @Before void inject_url() { customerServiceImpl.url = "http://..."; }

    @Test public void customerService_should_delegate_to_dataService() { ... }
}
Run Code Online (Sandbox Code Playgroud)

您可能已经注意到我正在使用直接访问该url字段,该字段可以是包可见的.这是一个实际注入URL值的测试解决方法,因为Mockito只注入了模拟.

  • @jhericks不,我打算在单元测试中删除与Spring相关的代码.我通常对此有一个强硬路线:我不鼓励在任何**单元测试**中使用Sprin等胶水代码.**单元测试**的目的是单独测试生产代码(这里是`CustomerServiceImpl`).这实际上似乎是该问题的作者的意图.如果由于某种原因需要弹簧,那么它开始是**积分测试**,它在测试中没有相同的含义. (5认同)

Jos*_*ust 5

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

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

我不喜欢仅为了测试而进行不同的自动装配(与我的代码库相比),但是从测试中更改被测类的替代方案简直是不道德的。