DTO Unit Testing

Is testing getters and setters appropriate and necessary?

I think they have no logic, and they cannot crash or throw any exceptions.

+5
source share
6 answers

You should not unit test DTO receivers and setters unless they contain complex logic that requires some testing.

+6
source

, , . - , , .

, , , - C & P:

private String foo;
private String bar;

String getFoo() {return foo;}
String getBar() {return foor;}

: , . ? - , , ? , ? - ?

+2

GOOS, , " " ( , DTO).

. " " , .

+2

... - >

setTest(String test) {
   this.test = test;
}

- , ( - , .):

String token=";";
   setTestTwo(String testTwo) {
   this.testTwo = testTwo + tokenString;
}
0

/ unit test :
1. Code coverage 2. Automated regression testing

, junit, , , .

0
source

Using modern tools, this is not so much effort:

import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor;
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEquals;
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanHashCode;
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanToString;
import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;

public class Test {

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testFlatFileReaderMetadata_Parameters() throws Exception {

        assertNotNull(new Test());

        assertThat(Test.class, allOf(hasValidBeanConstructor(), hasValidBeanEquals(), hasValidGettersAndSetters(),
                hasValidBeanHashCode(), hasValidBeanToString()));
    }
}
0
source

All Articles