Dubbo Application Unit test Environment (Springtest,powermock,mockito) (I.)

Source: Internet
Author: User
Tags static class zookeeper

Recently, Dubbo is frequently used in projects, and Java engineering uses dozens of associated systems (such as user authentication, basic services, customer platforms). These services are Dubbo services, we only provide an interface, services through the zookeeper registration, and to provide us with services. All of our projects are based on spring. Spring integrates Dubbo and can inject and use these external services.

But there's a problem with unit testing: Domain Model testing is not a problem, it's mostly your own code, and it's easy to test with a few mocks, but if I want to test the Application service layer (where the most external services are used), in many cases you need to start the spring environment. This requires the services of the external system to be loaded. The problem is the external service to our Jar package, only the service interface. If you load the spring context in the normal development environment at startup, it is obviously dependent on the external environment, and the spring context load will fail if the zookeeper is not started or if the computer is not networked, or if the associated system does not start, which is taboo for unit testing. If you use the spring configuration file for a dedicated unit test to remove the consumer configuration of the external associated system, startup will fail directly, let alone test.

Also write other questions, such as testing static methods, private methods, and how mock frameworks integrate with Springtest. Spring's AOP proxy class mocks some of the default implementations and tests how the database is selected. In short, the problem is very large. Well, it's time to get to the point.

1. Test static class, Private method problem

Simple word, with powermock. Powermock can do to modify the bytecode and change the behavior of the class, this is not much said, we search their own, official online examples are easy to understand. The current dependencies I have on Powermock,mockito in Maven are so joined:

<dependency> <groupId>org.powermock</groupId> <artifactid>powermock-api-mockito</ artifactid> <version>1.6.6</version> </dependency> <dependency> <groupid>o Rg.mockito</groupid> <artifactId>mockito-all</artifactId> <version>1.10.19</version&
			Gt </dependency> <dependency> <groupId>org.powermock</groupId> <artifactid>powermock -module-junit4</artifactid> <version>1.6.6</version> <scope>test</scope> &LT;/DEP endency> <dependency> <groupId>org.powermock</groupId> <artifactid>powermock-module- junit4-rule-agent</artifactid> <version>1.6.6</version> <scope>test</scope> </ dependency> <dependency> <groupId>org.powermock</groupId> <artifactid>powermock-modu Le-junit4-rule</artifactid> <version>1.6.6</version> <scope>test</scope> </dependency> <depende ncy> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.agent</artifactId> &LT;CL Assifier>runtime</classifier> <version>0.7.9</version> <scope>test</scope> ;/dependency>


The last words this jacoco is not a mock dependency, is a test coverage of plug-ins. Also recommended for everyone to use, haha.

2.powermock and Springtest in conjunction with the problem

The first problem was solved, yes. The second question comes. The runner of the spring Standard is springjunit4classrunner, and if you use this runner, then Powermock @prepairfortest will not be able to use (that is, the key to static mocks, private method mocks), So if you want to use both static and private method mocks you must use the Powemock runner, but how do you start the spring context?

After some lookup, finally solved the problem, the method is to use the Powermock agent, on the test class add such annotations:

@PowerMockIgnore ({"java.lang.management.*", "javax.management.*", "javax.xml.*", "org.xml.sax.*", " Org.apache.xerces.* "," org.w3c.* "})
@RunWith (powermockrunner.class)
@PowerMockRunnerDelegate ( Springjunit4classrunner.class)
@ContextConfiguration (locations = "Classpath:meta-inf/spring/test-spring.xml") )

Runner uses Powermockruner (the value of the Runwith annotation) and uses the proxy provided by Powermock to use the Springjunit4classrunner @PowerMockIgnore the role is to ignore some powermock used ClassLoader class, if not used, the startup use case will be an error.

Classpath:meta-inf/spring/test-spring.xml is a specialized spring configuration file for unit testing, and is somewhat different from the configuration used by field codes. I put this file in the/test/resources/spring/directory.

Here, a basic configuration based on powermock,springtest and Mockito is done. Well, the first part is here first. Cond

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.