[SpringBoot]Admin-project Test Guide

Jiyun Park
4 min readAug 29, 2019

This is a sample script for SpringBoot practice. I recorded it to fully understand what I learned and did myself. It will remind me of SpringBoot development process.

Caution! This is my personal opinion so it’s unsystematic and definitely informal.

[Development environment]

MacOS / IntelliJ / MySQL

[Practicing…]

  • SpringBoot version 2.1.7
  • MySQL Workbench :: Database, ERD
  • JPA :: Object-oriented

Development Process

It includes contents only about Test folder. Let’s go to see previous post about Main folder!

👩🏻‍💻click here ➡ https://medium.com/@JynnPark/springboot-admin-project-main-folder-guide-7173135d0f43

0. Purpose

I made 1 controller, 7 entities and 7 repositories in /Main folder last time. I wanted to test whether each repository successfully runs well. I would like to test CRUD methods.

1. Test set

Testing Library :: Arquillian Junit4

Basic annotations are @SpringBootTest , @RunWith(SpringRunner.class) and all RepositoryTest Classes are extended from main application test.

I put @Entity annotation into entity Class in advance, so I adopted @Autowired actively.

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserRepositoryTest extends AdminApplicationTests {

@Autowired
private UserRepository userRepository;

@Test
public void create(){
...
}
...
}

2. Fields in common : createdAt, createdBy, updatedAt, updatedBy

I realized that all entity Classes have some fields in common. It means that I have to set those 4 objects every time I make a new object. It seemed inefficient.

@CreatedDate
private LocalDateTime createdAt;

@CreatedBy
private String createdBy;

@LastModifiedDate
private LocalDateTime updatedAt;

@LastModifiedBy
private String updatedBy;
Item.java / User.java

In that case, I found a solution in LoginUserAuditorAware.java Class and JpaConfig.java Class. They support automatic setting. It turned into be more simple and clear code.

  • .setCreatedAt(LocalDateTime.now())
  • .setCreatedBy(“AdminServer”)
  • .setUpdatedAt(LocalDateTime.now())
  • .setUpdatedBy(“AdminServer”)
LoginUserAuditorAware.java / JpaConfig.java

Compared both entity Class and test Class, what did I create in person ? account, password, status, email, phoneNumber, registeredAt (5 in total). How about others ? createdAt, createdBy, updatedAt, updatedBy ? I solved them by JPA annotations.

  • @EntityListeners(AuditingEntityListener.class)
  • @CreatdDate , @CreatedBy, @LastModifiedDate, @LastModifiedBy
  • @Component
  • @Configuration, @EnableJpaAuditing

Now, I’ve done with several JPA annotation options.

3. Create()

UserRepositroyTest.java > create method

I began to handle CRUD methods and the point is that after creation of ‘user’ object, save it as a new object ‘newUser’ in userRepository. It finally reaches to database because it goes through @Autowired userRepository ~ @Entity User ~ datasource.url routes.

I checked it through Assert(org.junit) and it saved in MySQL as well.

User newUser = userRepository.save(user);
Assert.assertNotNull(newUser);

4. update()

UserRepositroyTest.java > update method

Find a specific object that I wanted to update and receive it as a parameter ‘selectUser’. Save it in userRepository as well.

5. delete()

UserRepositoryTest.java > delete method

Find a specific object that I wanted to delete and receive it as a parameter ‘selectUser’. Delete it in userRepository.

6.read() :: I would like to call it Association relationship Assembly..

UserRepositoryTest.java > read method

It represents that all objects in association relationships could be linked together and used at the same time. JPA supports that one object loads data from another object although they are not connected directly each other. For example, I selected optionalUser object in userRepository and wanted to print out its item’s category title that optionalUser bought.

In 67 line, it printed out item’s category title through ‘Partner-object’ over ‘Item-object’ over ‘OrderDetail-object’ over ‘OrderGroup-object’ and finally over ‘User-object’. @ManytoOne & @OnetoMany magic..

And it reminds me of how important ERD-step is.

optionalUser.ifPresent(user -> {
System.out.println("Order user account : "+user.getAccount());
//association relationship
//user -< order_group -< order_detail >- item >- partner >- category

user.getOrderGroups().stream().forEach(orderGroup -> {
System.out.println("---------- Order info ----------");
System.out.println("Received name : "+orderGroup.getRevName());
System.out.println("Total price : "+orderGroup.getTotalPrice());


System.out.println("---------- Order details ----------");
orderGroup.getOrderDetails().forEach(orderDetail -> {
System.out.println("Order status : "+orderDetail.getStatus());
System.out.println("Order item : "+orderDetail.getItem().getName());
System.out.println("Partner corp. Call center number : "+orderDetail.getItem().getPartner().getCallCenter());
System.out.println("Partner corp. category : "+orderDetail.getItem().getPartner().getCategory().getTitle());
});
});

Next time, I will talk about layer separation.

👩🏻‍💻click here to see previous post about Main folder ➡ https://medium.com/@JynnPark/springboot-admin-project-main-folder-guide-7173135d0f43

👩🏻‍💻click here to see all project files ➡➡ https://github.com/jyuunnii/SpringBoot_project_admin.git

--

--