摆弄 Mockito 来实现我的服务的单元测试,但由于某种原因,我无法通过我的厚脑袋来理解这一点。我的测试通过了,但我不相信我做对了。
这是我测试 count() 方法的示例。该方法只是将调用转发到其存储库,我不想验证仅此而已,没有其他任何事情发生。这就是我所拥有的:
@RunWith(MockitoJUnitRunner.class)
public class PersonServiceImplTest {
@Mock
private PersonRepository personRepository;
@InjectMocks
private PersonServiceImpl personService;
@Test
public void testCount() {
when(personRepository.count()).thenReturn(2L);
long count = personService.count();
assertEquals(2L, count);
verify(personRepository).count();
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试通过了,但我有一些问题。
是否需要 assertEquals?据我了解,无论我作为方法存根 (.thenReturn(value..)) 的预期结果,都将始终是返回的值。或者在这种情况下它可能是其他东西?
我需要验证吗?我觉得我这样做是因为我想验证 personRepository.count() 是否真的被调用了。或者当我也有 assertEquals() 时那是多余的吗?
我需要 assertEquals 和 verify 吗?
最后,我这样做对吗:)
谢谢
我正在使用 thymeleaf 作为模板引擎开发 Spring Boot 应用程序。我有这种情况,我需要显示一个列数可能不同的列。这是我到目前为止所拥有的:
<table class="table table-hover" th:if="${d.hasRecords()}">
<thead>
<tr>
<th:block th:eacth="h : ${d.header}">
<th th:colspan="${d.header.length}" th:text="${h}">Header Field</th>
</th:block>
</tr>
</thead>
<tbody>
<tr th:each="record : ${d.records}">
<th:block th:each="field : ${record}">
<td th:text="${field}">Dataset Field</td>
</th:block>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
变量header是标准 String[] 数组,而records对象是字符串数组列表。该表显示表正文,但不显示表头。唯一的区别(据我所知)是主体是嵌套循环,而标题不是。有人可以帮我解释一下为什么标题不会显示吗?并且头数组不为空或者null
我知道这个问题已经一遍又一遍地提出,并且有几种解决方案。除了建议您为此编写自己的配置bean的程序外,我尝试了其中的几种方法。我不想做所有这些事情,只是为了显示一个缝制过度杀伤力的小图标。但是我无法使其正常工作。这些是我到目前为止尝试过的解决方案。
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />这些都不起作用。
在浏览器中检查页面时,尽管没有显示任何图标,有时我还是没有打印出任何错误,或者我收到一条错误消息,说它GET http://localhost:8080/myapp/favicon.png 404 ()在将类型引用为JSON(我觉得很奇怪)。
我这里的想法不多了,因此如果有人可以告诉我为什么这行不通,请告诉我。我也许忘记了那些神奇的弹簧注解之一吗?这就是我的主要班级。
@SpringBootApplication
@ComponentScan
@Configuration
@EnableWebMvc
public class JobengineMonitorApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(JobengineMonitorApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用thymeleaf作为模板引擎