Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how junit test getter and setter

public class PlainOldJavaObjectTest {

    @Test
    public void testSetter_setsProperly() throws NoSuchFieldException, IllegalAccessException {
        //given
        final PlainOldJavaObject pojo = new PlainOldJavaObject();

        //when
        pojo.setValue("foo");

        //then
        final Field field = pojo.getClass().getDeclaredField("value");
        field.setAccessible(true);
        assertEquals("Fields didn't match", field.get(pojo), "foo");
    }

    @Test
    public void testGetter_getsValue() throws NoSuchFieldException, IllegalAccessException {
        //given
        final PlainOldJavaObject pojo = new PlainOldJavaObject();
        final Field field = pojo.getClass().getDeclaredField("value");
        field.setAccessible(true);
        field.set(pojo, "magic_values");

        //when
        final String result = pojo.getValue();

        //then
        assertEquals("field wasn't retrieved properly", result, "magic_values");
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: android get last crash adb 
Java :: Java alt f4 
Java :: array of number arrays java 
Java :: Java Longest String In String Array 
Java :: public class in java 
Java :: chenge font android studio 
Java :: generic classes in java 
Java :: uses or overrides a deprecated API. 
Java :: There is no client authentication. Try adding an appropriate authentication filter 
Java :: binary to octal conversion java program 
Java :: Program to remove duplicates in an ArrayList 
Java :: logging in java 
Java :: basic java programs 
Java :: how to add to an arraylist java 
Java :: room library android 
Java :: hashmap sort ascending 
Java :: android dialogfragment fullscreen stack overflow 
Java :: how to add a listener to a toggle group radio buttons javafx 
Java :: java input - how to read a string 
Java :: minimum and maximum in array in java 
Java :: How to read and write XML files 
Java :: java 8 stream option 
Java :: Java Access ArrayList Elements 
Java :: java polymorphism 
Java :: deserialize list jackson 
Java :: java method overriding 
Java :: color class android 
Java :: java Convert a string IPv4 IP address to the equivalent long numeric value. 
Java :: how to change resource color to int color in android 
Java :: resizing ImageIcon in JButton java 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =