In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to do unit testing in Spring Boot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is Spring Boot?
Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial building and development process of new Spring applications. The framework is configured in a specific way so that developers no longer need to define a templated configuration. To put it simply, Spring Boot is not really a new framework, it configures many frameworks by default, just like maven integrates all the jar packages and Spring Boot integrates all the frameworks.
Second, why use Spring Boot?
In fact, there is only one reason, elegant, clear, simple is better than clumsy, vague, complex, this is the core competitiveness of Spring Boot!
III. Quick introduction
(1) maven construction project
1. Visit http://start.spring.io/
2. Select the build tool Maven Project, Spring Boot version 1.3.6 and some basic engineering information, and click "Switch to the full version." java version selection 1.7.
3. Click Generate Project to download the project package
4. After decompressing, use eclipse,Import-> Existing Maven Projects-> Next-> Select the decompressed folder-> Finsh,OK done!
(2) introduction to the project structure
Generally speaking, the infrastructure of Spring Boot consists of three files:
Src/main/java program development and main program entry
Src/main/resources profile
Src/test/java test program
In addition, the directory results recommended by spingboot are as follows:
Root package structure: com.example.myproject
Com
+-example
+-myproject
+-Application.java
| |
+-domain
| | +-Customer.java |
| | +-CustomerRepository.java |
| |
+-service
| | +-CustomerService.java |
| |
+-controller
| | +-CustomerController.java |
| |
1. Application.java recommends putting it under the following directory, which is mainly used to do some framework configuration.
2. Domain directory is mainly used for entity (Entity) and data access layer (Repository).
3. The service layer is mainly business class code.
4. Controller is responsible for page access control.
Using the default configuration can save a lot of configuration, and of course you can change it according to your preference.
Finally, start the Application main method, and a java project is set up!
(3) introduce web module
1. Add modules that support web to pom.xml:
Org.springframework.boot
Spring-boot-starter-web
There are two modules by default in the pom.xml file:
Spring-boot-starter: core modules, including autoconfiguration support, logging, and YAML
Spring-boot-starter-test: test module, including JUnit, Hamcrest, Mockito.
2. Write controller content:
@ RestController
Public class HelloWorldController {
@ RequestMapping ("/ hello")
Public String index () {
Return "Hello World"
}
}
@ RestController means that all the methods in controller are output in json format, so you don't have to write anything about jackjson configuration!
3, start the main program, open the browser to access http://localhost:8080/hello, you can see the effect, it is so simple!
(4) how to do unit test
Open the test entry under src/test/, write a simple http request to test; use mockmvc, use
MockMvcResultHandlers.print () prints out the execution result.
@ RunWith (SpringJUnit4ClassRunner.class)
@ SpringApplicationConfiguration (classes = MockServletContext.class)
@ WebAppConfiguration
Public class HelloWorldControlerTests {
Private MockMvc mvc
@ Before
Public void setUp () throws Exception {
Mvc = MockMvcBuilders.standaloneSetup (new HelloWorldController ()) .build ()
}
@ Test
Public void getHello () throws Exception {
Mvc.perform (MockMvcRequestBuilders.get ("/ hello") .accept (MediaType.APPLICATION_JSON))
.andexpect (MockMvcResultMatchers.status () .isOk ())
.andDo (MockMvcResultHandlers.print ())
.andReturn ()
}
}
(5) debugging of the development environment
Hot start is already very common in normal development projects. Although during the development of web projects, changes to project startup and restart always report an error, springBoot supports debugging very well and can take effect in real time after modification. You need to add the following configurations:
Org.springframework.boot
Spring-boot-devtools
True
Org.springframework.boot
Spring-boot-maven-plugin
True
This module is disabled when running in a full packaged environment. If you launch the application with java-jar or with a specific classloader, it will consider this to be a "production environment".
This is the end of "how to do Unit testing in Spring Boot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.