Work summary (junit, mockito, powermock, webservice)

Introduction:
     Some time ago, I wrote some articles related to simple examples of Netty communication. I plan to read the source code of netty. After downloading the source code of netty, I read the test class of Netty. I don't know how to run it. I am ashamed . knowledge, this article is mainly a summary of recent work. The main involved Junit, Mockito and PowerMock; WebService (JAX-WS, Apache CXF), I plan to write a blog to summarize, there is not enough time, the test project of the related concepts mentioned above, the github that has been uploaded, interested can check it out Look, there are specific instructions and precautions in the project.

Introduction to Junit
reference: http://huihai.iteye.com/blog/1986568 ,
http://blog.csdn.net/chenleixing/article/details/44259453
In junit3, if a class is a test class, it must inherit the class TestCase. If a method is a test method, the method must start with testXX. If you want to specify a test method to run an initialization method before running, The name of this method must be setUp. If you want to run a method that releases resources after a test method runs, the name of this method must be tearDown. The difference between junit4 and junit3 is quite obvious. In junit4, it basically depends on annotations. The test method is identified by @Test, the initialization method is identified by @Before, and the method of releasing resources is identified by @After, but in order to let junit4 The test class can also be used in junit3. It is customary to name the initialization method setUp and the method to release resources tearDown. Test methods in Test generally start with Test. The method marked as Before annotation will execute the methods marked as @After and @Before every time the test class is run. If we want to run multiple test instances at once, we can add the instances to a TestSuite.
JUnit4 annotations:
1. @Test : Test method, the method that the test program will run, followed by parameters to represent different tests, such as (expected=XXException.class) exception test, (timeout=xxx) timeout test
2. @Ignore : Ignored test methods
3. @Before: run before each test method
4. @After: run after each test method
5. @BeforeClass: run before all tests start
6. @AfterClass: run after all tests end
7. @ RunWith(Suite.class): Test class running mode
8. @SuiteClasses: Test suite annotation

junit test project address: https://github.com/Donaldhan/junit-demo Introduction to Mockito Reference: http://www.jianshu.com/p/77db26b4fb54 What is mock?     Outside the world of software development, the word "mock" means to imitate or emulate. Therefore, "mock" can be understood as a substitute, a substitute. When "mock" is mentioned in software development, it is usually understood as a mock object or Fake. Why do you need Mock?     Mock is to solve the problem that units are difficult to be tested due to coupling. So the mock object is part of the unit test. What is the benefit of Mock? Create tests in advance, TDD (Test Driven Development)         is the biggest benefit. If you create a mock then you can write Service Tests before the service interface is created so that you can add tests to your automated test environment during development. In other words, mocking enables you to use test-driven development. Teams can work in parallel         this is similar to the point above; creating tests for code that doesn't exist. But what I talked about earlier is about developers writing test programs, here it’s about creating test teams. How do test teams create tests when there is nothing to test yet? Mock and test against mocks! This means that when the service excuses the need for testing, the QA team actually already has a complete set of testing components; there is no situation where one team is waiting for another to finish. This makes the benefit of simulation particularly prominent. You can create a verification or demo program.



   

  

   

   

  
        Since Mocks are so efficient, Mocks can be used to create a proof of concept, as a schematic, or as a demo program for a project you are considering building.
This provides you with a strong basis for deciding whether to proceed with the project next, but most importantly provides practical design decisions.
   The benefit of writing tests for inaccessible resources
        is not a real benefit, but a "life buoy" if necessary. Have you encountered such a situation? When you want to test a service interface, but the service needs to be accessed through a firewall, the firewall cannot be opened for you or you need authentication to access. When this happens, you can use MockService instead where you can access it, which is a "life buoy" function.
    Mock can be handed over to users
        In some cases, for some reason you need to allow some external sources to access your test system, like partners or customers. For these reasons, others can also access your sensitive information, and you may only want to allow access to part of the test environment. In this case, how to provide a test system to partners or customers for development or testing? The easiest is to provide a mock, either from your network or the client's network. The soapUI mock is very easy to configure, it can be run in soapUI or distributed as a war package to your java server.
    Isolating a System
        Sometimes you want to test a single part of a system without being affected by the rest of the system. Because other system parts will interfere with the test data, it will affect the test conclusions obtained from the data collection. Using mocks you can remove mocks of system dependencies except for the parts that need to be tested. When these mocks are isolated, the mocks become very simple, reliable, fast and predictable. This gives you a test environment that removes random behavior, has repeating patterns, and can monitor specific systems. Introduction to PowerMock Quote:



http://www.jianshu.com/p/60309d71002d
Why use the Mock tool
When doing unit testing, we will find that the method we want to test will reference many externally dependent objects,
such as: (send email, network communication, remote services, filesystems, etc.). And we can't control these externally dependent objects. In order to solve this problem, we need to use Mock tools to simulate these externally dependent objects to complete unit testing.
What is PowerMock?
PowerMock is also a unit test simulation framework, which is an extension on the basis of other unit test simulation frameworks. By providing a customized class loader and the application of some bytecode tampering techniques, PowerMock now has powerful functions such as mocking support for static methods, constructors, private methods and Final methods, and the removal of static initialization procedures. Because PowerMock fully uses the same API as the extended framework when extending functionality, developers familiar with the mocking frameworks supported by PowerMock will find PowerMock very easy to use. The purpose of PowerMock is to achieve additional functionality by adding very few methods and annotations to the currently familiar interface.

Mock and PowerMock test project address: https://github.com/Donaldhan/mockito-demo

WebService:
Reference:
http://www.cnblogs.com/firstdream/p/5575928.html ,
https://my.oschina. net/u/1757458/blog/365588 ,
https://my.oschina.net/xpbug/blog/224912 ,

JAVA6 (JAX-WS) specification:
JAX-WS2.0 (JSR 224) is Sun's new web services protocol stack. There are three WebService specifications in JAVA, namely JAX-WS (JAX-RPC), JAX-RS, JAXM&SAAJ. JAX-WS (Java API For XML-WebService), the version that comes with JDK1.6 is JAX-WS2.1, and its underlying support is JAXB. The early JAVA Web service specification JAX-RPC (Java API ForXML-Remote Procedure Call) has been replaced by the JAX-WS specification. JAX-WS is an evolved version of JAX-RPC, but JAX-WS is not fully backward compatible with JAX- RPC. JAX-RS is a new technology introduced in JAVA EE6. JAX-RS, the Java API for RESTful Web Services, is an application programming interface in the Java programming language that supports the creation of Web services according to the Representational State Transfer (REST) ​​architectural style. JAX-RS uses the Java annotations introduced in Java SE5 to simplify the development and deployment of web service clients and servers. Spring boot is a lightweight implementation of JAX-RS.
The composition of JAX-WS
1.SEI, the full name of Service Endpoint Interface or Service Endpoint Implementation. It is the class that server-side and client-side development really need to touch.
2.JAX-WS RI, the full name is JAX-WS Reference Implementation. This is the implementation of JAX-WS. The opposite should be JAX-WS and JAXB API.
3.JAXB, the full name of Java Architecture for XML Binding. This is a standard for mapping Java classes and XML schemas to each other. This standard can convert Java instances to XML and XML to Java instances.
4. SAAJ, the full name is SOAP with Attachment API for Java. This is a class library for parsing and generating SOAP protocol data.

WEB SERVICE (SOAP)
One of the most basic purposes of Webservice is to provide the ability to work together with different application systems on different platforms. A Web service is an application that exposes an API that can be called through the Web to the outside world. SOAP is a simple, xml-based, lightweight protocol for exchanging structured and typed information on the web. Soap request is a special version of HTTP POST, following a special xml message format Content-type is set to: text/xml Any data can be xml.
RESTFUL
REST (Representational State Transfer) is a lightweight Web Service architecture that can be fully implemented through the HTTP protocol. Its implementation and operation are more concise than SOAP and XML-RPC, and it can also use cache to improve the response speed, and its performance, efficiency and ease of use are better than SOAP protocol.
The operations of the REST architecture on resources include the operations of obtaining, creating, modifying and deleting resources, which correspond to the GET, POST, PUT and DELETE methods (Verb) provided by the HTTP protocol.
The difference between SOAP and HTTP
Why learn web services? Most external interfaces will implement web service methods instead of http methods. If you don't, there is no way to connect.
Are web services better than http (post/get)?
1. The methods implemented in the interface and the required parameters are clear at a glance . 2.
Don't worry about the capitalization problem
. ... is web service faster than http (post/get)? There may be some slowdown in speed due to xml parsing. Can web service be replaced by http (post/get)? It is completely possible, and the current open platforms are implemented using HTTP (post/get). The difference between Restful and SOAP Security: SOAP will be better than restful in terms of efficiency and ease of use (REST is better) Maturity (in general, SOAP is better than REST in maturity) JAX-WS test project address: https:/ /github.com/Donaldhan/jws-demo Apache CXF test project address: https://github.com/Donaldhan/cxf-demo












Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360136&siteId=291194637