Spring Framework - Basic CRUD Implementation Part 2
Create Test Methods
Using JUnit 4 we’ll create corresponding test methods in order to test database functionality.
Create a test class and add the following annotations:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(“file:src/main/webapp/WEB-INF/spring/root-context.xml”) OR define the root-context in classpath @Log4j
Note that ContextConfiguration can either recieve a absolute/relative path or a path for the schema derived from the classpath. In this case, root-cotext.xml’s direct path is given, as it is not defined in the application classpath (or even in it).
Autowire the mapper object so that it associates it with the mapper class. Now, write a test method:
//For select statements returning table data
@Test
public void testGetList() {
log.info(mapper.getList());
}
//For the other C,U,D operations:
@Test
public void testMethod() {
BoardVO vo = new BoardVO();
vo.setter(parameter);
mapper.operation(vo);
}
The test annotation can be commented out for methods that do not need testing. Additionally, other JUnit annotations can be attached for more specific types of testing (@Parameter, etc.).
Run Test Methods
Run test methods by running the test class as a JUnit test. The JUnit window will display successful/failed runs. What is important is to check if the SQL statements were correctly run to the DB. For this, check the log for input from the connector (jdbc.audit, jdbc.sqlonly to see the statement directly) or from direct DB logging (in the case of select statements).
Create Remaining CRUD operation classes, test methods
Create the remaining CRUD ops: create, update, delete in this case.
The mapped statements should correspond to *
Test methods should execute the methods in the mapper class in the syntax mapper.method(parameter). The method should only receive necessary parameters, i.e. using relevant data objects.
Now that the basic CRUD operations are implemented, it is possible to expand on this by implementing a frontend and other middleware.
-gonkgonk