@Service
public class TicketTemplatingService implements ITemplatingService{
@Autowired
private TemplateEngine templateEngine;
/**
* This method will return a ticket template
*/
@Override
public String buildHtmlTemplating(Object object, String templateName) {
Ticket ticket= (Ticket)object;
//Build the template
Context context = new Context();
context.setVariable("id", ticket.getId());
context.setVariable("date", ticket.getDate());
return templateEngine.process(templateName, context);
}
}
@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class TemplatingServiceTest {
@InjectMocks
private TicketTemplatingService ticketTemplatingService;
@Mock
private TemplateEngine templateEngine;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testHtmlTemplateReturnTheHtmlTemplate(){
Ticket ticket= new Ticket();
ticket.setId(1L);
Date date=new Date();
ticket.setDate(date);
Context context=new Context();
context.setVariable("id", 1L);
context.setVariable("date", date);
//Mock the process method of the templateEngine bean
when(templateEngine.process("TemplateName", refEq(context))).thenReturn("Html template result");
//Now we can test the method
String htmlTemplate=ticketTemplatingService.buildHtmlTemplating(ticket, "TemplateName");
assertThat(htmlTemplate).isEqualTo("Html template result");
}
}
而不是
@Autowired
private TemplateEngine templateEngine;
在您的服务中使用此接口。
import org.thymeleaf.ITemplateEngine;
@Autowired
private ITemplateEngine templateEngine;
并且在测试类中使用与Mock相同的类
@Mock
private ITemplateEngine emailTemplateEngine;
@Before
public void setup(){
@when(emailTemplateEngine.process(eq(TEMPLATE_USER_CREATION), any(Context.class))).thenReturn(userCreationHtml);
.
.
.
}
本文向大家介绍Spring Boot thymeleaf模板引擎的使用详解,包括了Spring Boot thymeleaf模板引擎的使用详解的使用技巧和注意事项,需要的朋友参考一下 在早期开发的时候,我们完成的都是静态页面也就是html页面,随着时间轴的发展,慢慢的引入了jsp页面,当在后端服务查询到数据之后可以转发到jsp页面,可以轻松的使用jsp页面来实现数据的显示及交互,jsp有非常强大的
本文向大家介绍SpringBoot使用Thymeleaf模板引擎访问静态html的过程,包括了SpringBoot使用Thymeleaf模板引擎访问静态html的过程的使用技巧和注意事项,需要的朋友参考一下 最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程。。。。。 一:创建spring boot的
本文向大家介绍使用Go进行单元测试的实现,包括了使用Go进行单元测试的实现的使用技巧和注意事项,需要的朋友参考一下 简介 日常开发中, 测试是不能缺少的. Go 标准库中有一个叫做 testing 的测试框架, 可以用于单元测试和性能测试. 它是和命令 go test 集成使用的. 测试文件是以后缀 _test.go 命名的, 通常和被测试的文件放在同一个包中. 单元测试 单元测试的格式形如: 在
我有这样一个简单的课程: 我想为它写一个测试,下面是一个框架: ErrorLogger类中的logger是由StaticLoggerBinder提供的,所以我的问题是-如何让它工作,以便那些检查“1*logger.error(u作为字符串)”可以工作?在ErrorLogger类中,我找不到一种恰当的方式来嘲笑那个记录器。我曾考虑过反射,并以某种方式访问它,此外,mockito注入也有一个想法(但如
问题内容: 我正在尝试创建一个简单的单元测试来测试我的表演功能。 我收到以下错误: 看来这不是控制器的范围吗? 这是我的控制器: 这是我的控制器单元测试: 问题答案: 为什么不简单地使用spyOn函数? 希望这可以帮助!
我正在为我的应用程序创建一个Spring BootAPI。我试图使用mockito对我的服务实现进行单元测试,以模拟出细节。该服务将向数据库添加一个新的构建实体。下面是服务实现和测试实现。楼宇服务: BuildingServiceImpl_UT