How to Use JUnit4 and intellijjunit4 in IntelliJ IDEA

Source: Internet
Author: User

How to Use JUnit4 and intellijjunit4 in IntelliJ IDEA
ZookeeperBackgroundRecently, I participated in an Anroid medical project. There are many basic classes and Communication classes at the bottom of the project, and many complicated byte operations include multi-thread synchronization and state machine processing. Such a project is necessary to do TDD, and the risk should be reduced as far as possible in the early stage of the project. Now the problem is that I am using an Android project developed by IntelliJ. At the beginning, I really don't know how to get started. After referring to some documents, I summarized the steps for configuring the JUnit test environment in IntelliJ idea, hoping to help you.
Environment:Operating System: Windows 7IntelliJ Version: 13.1 Android sdk api: 19 Java Version: 1.7 JUnit 4 sub-Version: JUnit 4.11 (built-in IntelliJ)

Steps:1. Make the following changes in the AndroidManifest. xml file of the project:Add under application Node
<Uses-library android: name = "android. test. runner "/>, add the following content under the root node: The targetPackage is the same as the package name of your project (see your AppManifest. xml file top:


2. Add the following jar package reference for your project1. junit-4.112, harmcrest-core-1.33, annotations
3. Adjust the reference sequence of the project to the jar package
This adjustment is made because the Android API we use contains JUnit 3.8. The default SDK is placed at the beginning, otherwise Junit in the code will be considered as Junit 3.8, then you can only use the Junit 3 test method.
4. In the project directory tree, create a directory at the same level as src to store all the test classes.For simplicity, I use tests.
In addition to creating directories, you also need to specify the newly created Directory As the Test Root Directory: Right-click the tests Directory and select 'Mark Directory As '---> 'test Sources root'

5. Create Your Test Category directory and specific test classes under your test root directory.In the Test class, all you need is to add @ Test annotation to the Test method.
6. Run your test1. On the code editing page, right-click your test method body and choose JUnit from the shortcut menu.
This method is very suitable for scenarios where you have just finished the test and need to see the running results of a test.
There is also a way to run a test independently by using the shortcut key. We only need to switch the text focus to a test method, then, press Ctrl + Shift + F10 to achieve the same test effect as above.
2. Edit the running Configuration and click the Configuration button. On the displayed page, follow these steps to create a JUnit running Configuration.


Make a simple configuration in the pop-up configuration window. For detailed configuration details, see IntelliJ IDEA's official document on JUnit support. This method is suitable for the scenario of performing tests in batches.


Review the usage of JUnit31. Let your test class inherit from Testcase2, and each test method name must be prefixed with lowercase test. Note: IntelliJ IDEA supports JUnit4 internally, and the Android SDK also supports JUnit3 at least, therefore, you do not need to perform dependency control on JUnit from Maven. Unless you need to use a feature in the latest JUnit.

Update:: After JUnit4.0 is enabled, there will be no way to run the test classes (such as AndroidTestCase) that involve the Android lifecycle in the project. The reason is that AndroidTestCase is supported by the SDK, but the SDK references the JUnit3.8 class library. I understand that if you reference JUnit4.0, therefore, the Android-specific JUnit3.8 test dependencies will not be loaded during runtime. An error will be reported when the test runner tries to load this type of test. Therefore, if you want to test classes related to the Android lifecycle in your project, you can only discard JUnit4 in the project.

Refer:Enable Annotationshttps: // www.jetbrains.com/idea/help/enabling-annotations.html in the IntelliJ IDEA Project
IntelliJ IDEA official document https://confluence.jetbrains.com/display/IntelliJIDEA/Unit+Testing+and+Code+Coverage for JUnit support
Sample project https://github.com/JetBrains/intellij-samples/tree/master/unit-tests-example for JUnit testing provided by IntelliJ IDEA

How to configure JUnithttp: // blog.csdn.net/huangbiao86/article/details/6709742 in ADT
JUnit4 Test Description: http://blog.csdn.net/xuxuxiaojie/article/details/5628671
JUnit4 is used in IntelliJ IDEA (there should be three articles, but only the last two articles are visible. Maybe the first article will be configured, but not visible-a little pity) http://blog.16.com/xianfuying@126/blog/static/2196000520110932132970/
Preliminary Exploration of Android unit test -- Instrumentationhttp: // www.oschina.net/question/54100_27061
Infinitesthttp: // infinitest. github. io/
A tutorial http://www.cnblogs.com/draem0507/archive/2013/04/25/3043557.html using JUnit in ADT
An old programmer Android test-Driven Development Practice Series 3 (mainly focused on interface interactive testing) http://blog.csdn.net/yt7589/article/details/12427185http://blog.csdn.net/yt7589/article/details/12427185http://blog.csdn.net/yt7589/article/details/12445283






Zookeeper

Related Article

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.