How to use JUNIT4 in IntelliJ idea

Source: Internet
Author: User

 backgroundRecently participated in a anroid medical project, in which there are very many basic classes and communication classes at the bottom of the project, and many involve complex byte operations as well as multi-thread synchronization and state machine processing. It is necessary to do TDD on such a project, and to reduce the risk as much as possible in the immediate period. The problem now is that I'm using the Android project developed by IntelliJ, and I really don't know how to start. After some of the information has been tested. Summarize the steps in IntelliJ to configure the JUnit test environment, and hope it will help you.


Environment: operating system: Windows 7IntelliJ Version number: 13.1Android SDK api:19java version:1.7junit 4 child version number: JUnit 4.11 (IntelliJ comes with)

steps: first, in the project's Androidmanifest.xml file, such as the following changes: add under the application node
<uses-library android:name= "Android.test.runner"/> Add the following to the root node, for example: The Targetpackage is the same as the package name of your project (see the top of your appmanifest.xml file):


second, for your project to join for example the following JAR package reference 1, junit-4.112, harmcrest-core-1.33, annotations
Third, adjust the project to the Jar package reference order
This adjustment is made because JUnit 3.8 is included in the Android API we use, and the default SDK is on top. Otherwise, junit in the code would be considered JUnit 3.8, and you would only be able to use the JUnit 3 test method.
Iv. in the project folder tree, create a folder with SRC peers to hold all the test classes. for the sake of simplicity, I am using tests.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbwlzc2f1dhvtbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
In addition to creating folders. We also need to specify the newly created folder as the test root folder: Right-click the tests folder and select ' Mark Directory as '---> ' Test Sources root '

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbwlzc2f1dhvtbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

in your test root folder to create your test classification folder and detailed test class within the test class you only need to add @test labeling to the test method.


Six, carry out your test 1, in the code editing interface, in your test method body right click, in the popup menu selected in JUnit way to execute
It's a good way to just finish the test. A scenario in which a test's execution result needs to be seen.
Another way of using shortcut keys to perform a test alone is that we just need to switch the text focus to a test method, and then use the combination key CTRL+SHIFT+F10 to achieve the same test results as above.
2, edit the configuration click Configurationbutton, in the pop-up interface, such as the following steps to create a JUnit execution configuration


In the popup configuration form do a simple configuration.

For detailed configuration details, please refer to the section "IntelliJ Idea's official documentation for JUnit support" in this section. This is a good way to run test scenarios in batches.


recall the use of JUNIT3 1, let your test class inherit from Testcase2, each test method name must be prefixed with a lowercase test note: IntelliJ idea has internal support junit4,android At least JUNIT3 is supported in the SDK. Therefore, there is no need to control junit from Maven.

Unless you need to use one of the features in the latest junit.

Update:2014-10-16: Test classes involving the Android life cycle in the project after opening the JUnit4.0 feature (for example, Androidtestcase) will have no way to execute. The reason for this is that Androidtestcase is supported by the SDK, But the SDK refers to the JUnit3.8 class library, my understanding is that you are referring to JUnit4.0, then at execution time androidtestcase these Android-specific test-dependent JUnit3.8 will not be loaded during execution. The test actuator tries to load the test in such a case that an error occurs. So, assuming you want to test the classes associated with the Android life cycle in your project, you can simply abandon JUNIT4 in your project.

References:Open annotationshttps://www.jetbrains.com/idea/help/enabling-annotations.html in IntelliJ Idea's project
IntelliJ Idea's official documentation for JUnit support Https://confluence.jetbrains.com/display/IntelliJIDEA/Unit+Testing+and+Code+Coverage
IntelliJ Idea offers the JUnit test sample project Https://github.com/JetBrains/intellij-samples/tree/master/unit-tests-example

How to configure junithttp://blog.csdn.net/huangbiao86/article/details/6709742 in ADT
JUNIT4 Test Description: http://blog.csdn.net/xuxuxiaojie/article/details/5628671
The use of JUNIT4 in IntelliJ idea (there should be three articles, but only the next two are visible.) Maybe the first one will be configured, but not visible--a little pity. http://blog.16.com/[email protected]/blog/static/2196000520110932132970/
Research on Android Unit Test--instrumentationhttp://www.oschina.net/question/54100_27061
IntelliJ idea and ADT's continuous test plugin infinitesthttp://infinitest.github.io/
A tutorial for using JUnit in ADT http://www.cnblogs.com/draem0507/archive/2013/04/25/3043557.html
An old program Ape's Android Test drive development Practice Series 3 (main side heavy Interface interactive test) http://blog.csdn.net/yt7589/article/details/12427185http://blog.csdn.net /yt7589/article/details/12427185http://blog.csdn.net/yt7589/article/details/12445283








How to use JUNIT4 in IntelliJ idea

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.