简体   繁体   中英

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. I passed empty DTO, and entity created by 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.

Why I need to test methods like this?

  • The methods that set data from entity to DTO has small formatting specyfic fields. 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. (Cleanest way)
  • Overriding a matching toString() then compare the resulting strings.
  • 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).

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.

If you want a more generic approach there are additional libraries like assertj's 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. Like splitting a string etc.

This makes a unit test meaningful: verify if the fields in the DTO are filled as expected. So don't just test on notNull but test on

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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