This piece of code is an example on how to apply CRUD operations on a MySql database from Java through jdbc.
The code is written for 2. semester on the Datamatiker education in Lyngby.
The code is NOT refactored.
These exercises are aimed at adding integration tests to the sportsclub application. The purpose is to test the datamapper methods, so that we can make sure that our database queries are working.
- Begin by cloning the project. Since todays project is hiding on a branch called
"integrationtest", you need to clone the specific branch like this:
git clone -b integrationtest git@github.com:jonbertelsen/dat2-flow3-sportsclub.git
- After cloning, remove the .git folder by typing
rm -rf .git
. - Open the project in IntelliJ
- We assume that you already have the sportsclub database on your MySql sever. Otherwise open the file "src/main/resources/sportsclub.sql" in mySql Workbench and execute it to create the sportsclub database.
- Update the MySql login credentials in the Main class to reflect your local system
- Create a new database called "sportsclub_test" on your MySql server Open the file src/main/resources/sportsclub_test.sql in MySql Workbench and execute it. You can read more about why and how in the file.
- Update the MySql login credentials in the MemberMapperTest.java file.
- Run Main and the tests, and get to know the code.
- Try to describe in your own word what happens in the @BeforeAll section. We suggest that you write a comment at the end of each line.
- Try also to describe what happens in the @BeforeEach section by commenting the code.
- Investigate each of the tests below, write your own comments,
and try to change the test to make them fail:
- testConnection()
- getAllMembers()
- getMemberById()
- deleteMember()
- insertMember()
- updateMember()
- Create a new test in which you will try to delete two members and find out if it went well.
- Create a new test in which you will delete a member with a member_id that does not exist. It could be member_id = 1234. Try to figure out how to test it correctly. For this to work, you will need to use assertThrows and take a close look at the exceptionhandling in the deleteMember method.
- Create a test in which you will try to insert a new member with illegal types. It could be that you try to insert a member with an illegal gender type. "x" for example. For this to work, you will need to use assertThrows.
- Yesterday we worked on adding a RegistrationMapper to the application in the exercises. In case you have made it, then add a new test class and test the mapper.