Skip to content

Instantly share code, notes, and snippets.

@sawaYch
Last active November 10, 2018 23:46
Show Gist options
  • Select an option

  • Save sawaYch/185841de5e89f875882c860c15a9f7a1 to your computer and use it in GitHub Desktop.

Select an option

Save sawaYch/185841de5e89f875882c860c15a9f7a1 to your computer and use it in GitHub Desktop.
Special way to test binding type (JavaFX and Junit)

Special way to test binding type (JavaFX and Junit)

Code of DataModel Class

The class data model is a binding type for javafx bar chart.

public static class DataModel {
    private final SimpleStringProperty title;
    private final SimpleStringProperty price;
    private final SimpleStringProperty url;
    private final SimpleStringProperty postedd;

    public DataModel(String _title, String _price, String _url, String _posted_date) {
        this.title = new SimpleStringProperty(_title);
        this.price = new SimpleStringProperty(_price);
        this.url = new SimpleStringProperty(_url);
        this.postedd = new SimpleStringProperty(_posted_date);

    }

    public String getTitle() {
        return title.get();
    }

    public void setTitle(String _title) {
        title.set(_title);
    }

    public String getPrice() {
        return price.get();
    }
    public void setPrice(String _price) {
        price.set(_price);
    }

    public String getUrl() {
        return url.get();
    }
    public void setUrl(String _url) {
        url.set(_url);
    }

    public String getPostedd() {
        return postedd.get();
    }
    public void setPostedd(String _posted_date) {
        this.postedd.set(_posted_date);
    }

}

Code of test case

package comp3111.webscraper;
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import static org.hamcrest.CoreMatchers.is;
import org.junit.Test;
import javafx.beans.property.SimpleStringProperty;
import comp3111.webscraper.Controller.DataModel;

@Test
public void dataBinding_test() {
	try {
		Class progClass = data.getClass();
		Field postedd_field = DataModel.class.getDeclaredField("postedd");
		postedd_field.setAccessible(true);
		SimpleStringProperty x = (SimpleStringProperty) postedd_field.get(data);
		SimpleStringProperty src1 = new SimpleStringProperty();
		x.bind(src1);
		String expected = "01/01/2001";
		src1.set(expected);
		String actual = data.getPostedd();
		assertThat(actual,is(expected));
	} catch (SecurityException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

References: http://www.51gjie.com/java/795.html

Using Java Reflection for private member

   @Test
    public void test_has_bar_selected() throws Exception {
    	Controller test = new Controller();
    	Controller test2 = new Controller();
    	List<Boolean> list = new ArrayList<>();
    	List<Boolean> list2 = new ArrayList<>();
    	list.add(true);
    	list2.add(false);
    	
    	Field field = test.getClass().getDeclaredField("bar_smVector");
    	field.setAccessible(true);
    	field.set(test, list);
    	
    	Field field2 = test2.getClass().getDeclaredField("bar_smVector");
    	field2.setAccessible(true);
    	field2.set(test2, list2);
    	
    	Method method = null;
    	Method method2 = null;
		method = test.getClass().getDeclaredMethod("has_bar_selected", null);
		method2 = test2.getClass().getDeclaredMethod("has_bar_selected", null);		
    	method.setAccessible(true);    	
    	method2.setAccessible(true);    	
    	Boolean result1;
    	Boolean result2;
		result1 = (Boolean) method.invoke(test, null);
		result2 = (Boolean) method.invoke(test2, null);
    	assertEquals(result1, true);
    	assertEquals(result2, false);
		
    }

References: https://www.geeksforgeeks.org/reflection-in-java/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment