Lesson 6 | Testing packaged Beans in the BeanBox |
Objective | Use the BeanBox to test a packaged Bean. |
Testing Packaged Beans in the BeanBox
To use a Bean with the BeanBox, you simply package the Bean in a JAR file and copy the file to the jars directory beneath the main Bdk directory. When the BeanBox is executed, it scans each JAR file in this directory and adds all of the Beans it can find to its ToolBox.
Question: How do I use the BeanBox in the JavaBeans Framework to test a packaged Bean?
To use the BeanBox in the JavaBeans Framework to test a packaged Bean, you can follow these steps:
- Create a new JUnit test class.
- Declare a BeanBox object.
- Instantiate the BeanBox object with the name of the packaged Bean class.
- Get a reference to the packaged Bean object using the BeanBox object.
- Set the properties of the packaged Bean object.
- Call the methods of the packaged Bean object.
- Assert the results of the packaged Bean object's methods.
Here is an example of a JUnit test class that uses the BeanBox to test a packaged Bean:
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.test.beans.City;
public class CityTest {
@Test
public void testSetAndGetPostOffices() throws IntrospectionException {
// Create a new BeanBox object.
BeanBox beanBox = new BeanBox();
// Instantiate the BeanBox object with the name of the packaged Bean class.
beanBox.setBeanClass(City.class);
// Get a reference to the packaged Bean object using the BeanBox object.
City city = (City) beanBox.getBean();
// Set the properties of the packaged Bean object.
city.setPostOffices(10);
// Call the methods of the packaged Bean object.
int postOffices = city.getPostOffices();
// Assert the results of the packaged Bean object's methods.
assertEquals(10, postOffices);
}
}
This test class will first create a new BeanBox object. Then, it will instantiate the BeanBox object with the name of the packaged Bean class, City. Next, it will get a reference to the packaged Bean object using the BeanBox object.
After that, the test class will set the properties of the packaged Bean object. In this case, the test class is setting the postOffices property to 10.
Next, the test class will call the methods of the packaged Bean object. In this case, the test class is calling the getPostOffices() method.
Finally, the test class will assert the results of the packaged Bean object's methods. In this case, the test class is asserting that the getPostOffices() method returns 10.
You can use the BeanBox to test all of the properties and methods of a packaged Bean. This can be a very useful tool for ensuring that your packaged Beans are working as expected.
Running BeanBox
The beanbox directory in your BDK installation contains two command scripts for running BeanBox.
The first one, run.bat, is for running BeanBox on Windows. The other, run.sh, is for running it on a UNIX system.
When you execute the appropriate command, either run or run.sh, BeanBox presents three separate windows.
The first one is a toolbox of the Beans that are available. The second is the main window.
This contains the form that Beans are dropped onto, as well as some menus for connecting Beans and reporting on them.
The last window is the property sheet. This window allows you to edit the properties of the currently selected Bean.