This article will skip the configuration of Spring Framework Environment basically because It’s not main focus of this and It’s too longer then the thing related to the spring test. If you want to know about those either then I recommend to read this document.
1. Add Dependency in your build.gradle
You should have all of the below dependencies for testing your spring project.
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'// It may be default setting. testCompile group: 'junit', name: 'junit', version: '4.12'// It may be default setting. testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'// Let's add this code. testCompile group: 'org.springframework', name: 'spring-test', version: '5.1.5.RELEASE'// Let's add this code.
And you should condsider the version of javax.sevlet dependency carefully. If your thing is lower than 2.5 then maybe It’s not working well when you test your project. and I don’t know why this happen but You have to add the ‘spring-jdbc’ dependency for testing.
2. Configure the HomeController.java
For testing, We will make a one controller thing in the project. and the code is like the below.
Stringres= name + " " + age + " " + major; return res; }
}
If you want to test whether hello function work well or not in this HomeController, then you always turn on the WAS in locally and you also must send some request to your controller. But It’s sometimes really tiresome. Surely, You can use the another service like postman for solving this issue. But I think to use the test code in project is really simple way to test the controller.
The above code is taken three parameters from someone who certainly doesn’t know. and return this parameter using the String type. and the url is only ‘/‘.
MockMvc Object is like browser. It can send the request to our controller. So We can test our code using this object without any server running now. This MockMvc Object is from the spring-test library. so whenever you want to use this, You should add the dependency of this. Actually You can see this code at the above related to build.gradle.
And You should know the truth whether to test the controller in spring framework is an unit test or intergration test. Many developers over the world said that It’s not Unit test because Unit test should be independent. but to test the controller is not. But I think this among all of the test technic is really important than others. Many problems usually were occured from the connection between client and server part.
1 2 3 4 5 6 7 8 9 10 11 12
mockMvc.perform(get("/") // You can insert the url you want to test.
.param("name", "kidongyun") // The part for adding the parameters. .param("age", "20") .param("major", "computer engineering"))
.andDo(print()) // This code can show the result of the MockMvc.perform() test.
.andExpect(status().isOk()); // status().isOk() means that we expect the success of the connection like '200' status code // so If this test has any problems then It will return the fail because of this code.
If your test is successed then you can take the result like the below picture. Your MockHttpServletResponse status is 200. Otherwise you forget one of the parameters at the above code like the below then you will take the failed result.
Actually according to my experience, When you configure your Spring project using the java configruation without xml code, The Controller Test code is little different. But if you don’t understand about these things, It may be going to feel too difficult.
And you should consider the version of the ‘spring-test’ dependency. in my case, I used 5.X.X version of this, But It’s not proper about the 4.X.X ‘Spring’ Environment. So First of all, Let’s see the version of libraries via the above code. these dependencies include the function of the ‘mybatis’. so don’t confuse about that.
6. Create Application Context directly at the Test Code.
Actually To use the application context used from production environment is sometimes too hard. If you project is too big, then whenever you do the test, you test code configure your all of things related to application context. so I will introduce the new way to solve these problems.
If the object you want to register to application context has another your custom object, then you should register that object either.
7. The way how to test Controller.
First of all, You can measure the result of the connection using this test code. That’s exactly status code. and Second, You can check whether the parameter types are correct or not if you want.