The project structure is a service provided by spring Boot to provide RPC interface, and Dubbo is used;
Recently need to use unit testing, found that the same card is there, the implementation of the test method, seemingly blocked;
Unit Test class:
@RunWith (Springrunner.class)
@SpringBootTest (webenvironment= SpringBootTest.WebEnvironment.NONE, classes = Application.class)
public class Tagtest {
@Test
public void Test () {
System.out.prin
Overview
The @SpringBootTest annotation is an annotation for testing that was introduced by Springboot since the 1.4.0 version. The basic usage is as follows: 1. Add maven dependencies
2. Write Startup entry class
@SpringBootApplication public
class Startupapplication {public
static void Main (string[] args) {
Springapplication.run (Startupapplication.class, args);
}
3. Writing the Controller class
@RestController public
class
layer of cityservice.The integration test scenario for this controller is as follows:@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class CityControllerWithRunningServer { @Autowired private TestRestTemplate restTemplate; @Test public void getAllCitiesTest() { String response = restTemplate.getForObject("/cities", String.class); Assertions.assertThat(response).co
next layer of cityservice.
The integration test scenario for this controller is as follows:
@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class CityControllerWithRunningServer { @Autowired private TestRestTemplate restTemplate; @Test public void getAllCitiesTest() { String response = restTemplate.getForObject("/cities", String.class); Assertions.assertThat(respo
@WebFluxTest to test. It provides functionality and is @WebMvcTest similar. And webtestclient can be used directly, has been automatically configured (auto-configured).Supports automatic configuration with @springboottest webtestclientWhen you use @SpringBootTest the with an actual server (which is, either Defined_port or Random_port), a webtestclient is available the same the T Estresttemplate is.Using @
First Maven wants to introduce spring-boot-starter-test this package.Look at a piece of code first @RunWith (Springrunner. Class ) @SpringBootTest (webenvironment = Webenvironment.random_port) public class MyTest { @Autowired private testresttemplate resttemplate; @Test public void Test () { this .resttemplate.getforentity" ( "/{username}/vehicle", String. class , "Phil" ); }} 1. @RunWith (Springrunner.class) tells JUni
{
tearDown();
} catch (Throwable tearingDown) {
if (exception == null) exception = tearingDown;
}
}
if (exception != null) throw exception;
}
This is an earlier version of the TestCase class, one of the implementations, which is basically consistent with what we call the idea.However, annotation-based implementations are provided by JUNIT4 and can be looked at in depth when interested.
KeywordsTestCase, Junit4testada
Unit tests, Global exceptionsOne, unit test1. Basic version1, the introduction of relevant dependenciesspringboot Program Test dependent, if it is automatically created project default add -Dependency>groupId>Org.springframework.bootgroupId>Artifactid>Spring-boot-starter-testArtifactid>Scope>TestScope>Dependency>2: Key Note:@RunWith @SpringBootTestImportjunit.framework.TestCase;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframew
Some time ago, a friend asked Springboot how to use unit testing, combined with the actual use of LZ company, here to describe the three kinds of unit testing methods.1. ConventionsThe unit test code is written in the Src/test/java directoryThe Unit test class is named *test, prefixed with the class name to be tested2. Unit test using mock modeThe Spring test Framework provides MOCKMVC objects that can be tested in MVC without the need for client-server requests, and can execute the controller's
1. Test-driven development (testing driven DEVELOPMENT,TDD) is a test case where we write our own expected results as required, and this test case is just beginning to be a failed test, with constant coding and refactoring, Finally, test cases are tested so that the quality and controllability of the software can be guaranteed. 2. Test dependencies, the package survives the test cycle, and the release does not contain jar packages. 1 Dependency>2 groupId>Org.springframework.bootgroupId>3
Tags: control word test IAT ESS src test for VAT star1. ConventionsThe unit test code is written in the Src/test/java directoryThe Unit test class is named *test, prefixed with the class name to be tested2. Unit test using mock modeThe Spring test Framework provides MOCKMVC objects that can be tested in MVC without the need for client-server requests, and can execute the controller's request completely on the server side, just as it does with the test servers.The test environment needs to be est
time to implement database operations. Second, integration with JdbcTemplate First, we use JdbcTemplate to access the database, only to demonstrate the insertion of data, as we have mentioned in the previous article, Spring Boot provides many starter to support different functions, To support jdbctemplate we just need to introduce the following starter: Now we can implement the data insertion by JdbcTemplate: Public interface Articledao {Long insertarticle (article article);} @Repository pub
A test class consists of the following two annotations:@RunWith (Springrunner. Class) @SpringBootTestThe interface can be injected directly into the test class:@ResourceMyServerMgr Myservermgr;Adding @test to a method means that it is a test method:@Test Public void = myservermgr.query (); Log.info ("Test query result is {}.") // use assertion to determine the result assertequals (result, "query result");}The behavior of the test method before and after execution can be expressed by means of
: rabbitmq: host: localhost port: 5672 username: guest password: guestDirectDirect mode generally only needs to define queue using the self-bring switch (defaultexchange) without binding the switch @Configurationpublicclass RabbitP2PConfigure { publicstaticfinal"p2p-queue"; @Bean publicqueue() { returnnewtrue); }}@RunWith(Springrunner.class)@SpringBootTest(classes = bootcoretestapplication.)class)@Slf4j Public class
Springboot Connect Redis and dynamically switch database
As we all know, Redis has a db, in the Jedis can use the Select method to dynamically select the Redis database, but in the stringredistemplate provided by Springboot does not have this method, Fortunately, stringredistemplate reserved a setconnectionfactory method, this article is to modify the connectionfactory so as to achieve dynamic switching database effect. springboot Connection Redis
Pom.xml file to introduce Spring-boot-starter-r
I. OverviewThis section focuses on getting the contents of the properties file.Two. Get the value of an attribute using an environment variableIn Springboot, all the scanned property files are stored in the environment variable, and the environment variable also places some other properties. Therefore, we can get the value of the property file from the environment variable.Jdbc.username=rootWe configured the above content in the Application.properties file.@
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.