如何在百里香测试用例中模拟@ environment.getProperty('test.test')

时间:2019-08-27 14:58:15

标签: spring-boot thymeleaf

下面的代码

它在我的应用程序中工作得很好,但是我在此thymeleaf html页面上有测试用例,并且在我的测试用例中抛出了未找到@ envioronment.getProperty的bean。

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "@environment.getProperty('css.specific.name')" (src/main/resources/templates/test_details.html)    

EL1057E: No bean resolver registered in the context to resolve access to bean 'environment'   

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ThymesheetTestSpringContext.class})
public class ViewTest {
    @Autowired
    private ThymeleafTestEngine testEngine;

    @Autowired
    private TestMessageSource messageSource;    

    String templatePath = "src/main/resources/templates/test_details.html";    
@Test
    public void shouldDisplayVehicleInformationForIVFP() throws Exception {
        helper.assertLocalization("test", "returned", "#test");
        helper.assertLocalization("test1", "Frame number:123", "#test1");
    }
} 
   public class ThHelper {

    private ThymeleafTestEngine testEngine;
    private TestMessageSource messageSource;

.....................................
private Callable<HtmlElements> view = () -> testEngine.process(templatePath, model);

    private Function<Map<String, Object>, HtmlElements> viewWithModel = (model) -> testEngine.process(templatePath, model);    
}

1 个答案:

答案 0 :(得分:1)

您必须autowire Environment bean,以便它可以具有如下所示的实例化对象。

@Autowired
Environment environment

您还可以通过@Value中的ReflectionUtils设置spring test framework带注释的字段

典型的setField如下所示:

ReflectionUtils.setField(mockedServiceWhichNeedsValueSet, nameOfFiledToBeSet, valueToBeSet);