简体   繁体   English

jUnit如何测试仅将数据从实体设置为dto的方法

[英]jUnit how to test methods that only set data from entity to dto

I've problems with test methods which only get data from entity and assign it to DTO.我在测试方法方面遇到了问题,这些方法只从实体获取数据并将其分配给 DTO。 I passed empty DTO, and entity created by entityMother.我通过了空的 DTO,以及由 entityMother 创建的实体。 The method works correctly, but I don't know how to make assertion for this.该方法工作正常,但我不知道如何对此进行断言。 I think that creating assertion for each value/property of this DTO is not a right way to test it.我认为为这个 DTO 的每个值/属性创建断言并不是测试它的正确方法。

Why I need to test methods like this?为什么我需要测试这样的方法?

  • The methods that set data from entity to DTO has small formatting specyfic fields.将数据从实体设置为 DTO 的方法具有小的格式特定字段。 Like splitting a string etc.像分割字符串等。
@Test
    public void shouldSetAvailabilities() {
        EditedTemplateShowDto editedTemplateDto = new EditedTemplateShowDto();
        productTemplateEditService.getAndSetAvailabilities(editedTemplateDto, editedProduct);

        //Here should be condition which check that the metod set data
    }

I just need to check that the method didn't throw any errors, and none of fields has assigned null value.我只需要检查该方法是否没有抛出任何错误,并且没有任何字段分配了空值。

The possible solutions are:可能的解决方案是:

  • You may serialize your objects to JSON then compare the resulting strings.您可以将对象序列化为 JSON,然后比较结果字符串。 (Cleanest way) (最干净的方式)
  • Overriding a matching toString() then compare the resulting strings.覆盖匹配的 toString() 然后比较结果字符串。
  • Put several assert condition using reflection (to check the variable name) in a test to check there are no any null value or not mapped value (Bad Practice).在测试中使用反射(检查变量名称)放置几个断言条件,以检查没有任何空值或未映射值(不良做法)。

Why I need to test methods like this?为什么我需要测试这样的方法?

First of all, you don't need to test anything.首先,你不需要测试任何东西。 If you feel the code is extremely straight forward then I would advice to just invest your time into something else.如果您觉得代码非常简单,那么我建议您将时间花在其他事情上。 Or perhaps write one sweeping test so at least you have code coverage (but IMO you'd be doing this more for the stats than actual quality of your product).或者也许编写一个全面的测试,这样至少你有代码覆盖率(但 IMO 你会做更多的统计而不是你的产品的实际质量)。

I just need to check that the method didn't throw any errors我只需要检查该方法没有抛出任何错误

That's easy!这很容易! Just write a unit test that calls the method.只需编写一个调用该方法的单元测试。 If the method throws any exceptions, the test will fail.如果该方法抛出任何异常,则测试将失败。 If you want to make your test method more intent-revealing, you could explicitly write it like:如果你想让你的测试方法更能揭示意图,你可以明确地写成这样:

try {
      productTemplateEditService.getAndSetAvailabilities(editedTemplateDto, editedProduct);
   } catch(Exception e) {
      fail("Should not have thrown any exception");
   }

But again, I'd only do this with methods I expect to throw exceptions (eg because they have exception paths or use other code/libraries that may throw exceptions).但同样,我只会使用我希望抛出异常的方法来执行此操作(例如,因为它们具有异常路径或使用其他可能抛出异常的代码/库)。 The default behavior of any unit test is to fail when it encounters an uncaught exception.任何单元测试的默认行为都是在遇到未捕获的异常时失败。

none of fields has assigned null value.没有任何字段分配空值。

The JUnit way is to use assertNotNull in your test method on any fields you want to ensure are not null. JUnit 方法是在您要确保不为空的任何字段的测试方法中使用assertNotNull

If you want a more generic approach there are additional libraries like assertj's hasNoNullFieldsOrProperties .如果您想要更通用的方法,还有其他库,例如 assertj 的hasNoNullFieldsOrProperties

assertThat(editedTemplateDto).hasNoNullFieldsOrProperties();

Or you could write your own code using reflection like:或者您可以使用反射编写自己的代码,例如:

for (Field f : editedTemplateDto.getClass().getDeclaredFields())
    assertNotNull(f.get(editedTemplateDto))

But I'd advice against this as it makes your test code harder to understand (and possibly brittle because reflection itself can be tricky)但是我建议不要这样做,因为它会使您的测试代码更难理解(并且可能很脆弱,因为反射本身可能很棘手)

The methods that set data from entity to DTO has small formatting specyfic fields.将数据从实体设置为 DTO 的方法具有小的格式特定字段。 Like splitting a string etc.像分割字符串等。

This makes a unit test meaningful: verify if the fields in the DTO are filled as expected.这使单元测试变得有意义:验证 DTO 中的字段是否按预期填充。 So don't just test on notNull but test on所以不要只测试 notNull 而是测试

Assert.assertEquals("FieldX has a wrong value","myexpectedValue",dto.getFieldX());

This way you test if the split logic behaves as expected.通过这种方式,您可以测试拆分逻辑的行为是否符合预期。

Also test it with null values in all optional fields to verify you don't get NullPointerException.还要在所有可选字段中使用空值对其进行测试,以验证您没有收到 NullPointerException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM