Android unit test-JUnit and androidjunit
With the development of testing in recent years, we often see recruitment information for testing engineers. There is a unit test such a JUnit in Java
Currently, Android is mainly written in Java, so in Android development, there will also be a unit test --- JUnit. Build an Android Development Environment
As you can see, A
JUnit's feature enhancement) two packages 2. Why to use JUnittest framework can help us to write the program for purposeful testing, can help us to minimize the code in the bug, is the program to achieve the desired effect, usually a complete project will contain thousands, or even more methods, then how to determine the results of these methods are what we want, it becomes Important. Of course, you can also create a lot of main functions, use a l
Developers
JUnit
Step-by-Step learning of JUnit
Author: micel casabianca
Use one of the most popular open resource testing frameworks to learn the unit test basics.
Using JUnit can greatly reduce JavaCodeMediumProgramNumber of errors. JUnit is a popular unit test framework used to perform
completed.
The JUnit test framework class structure is simple, consisting mainly of 3 classes, whose class diagram relationships are shown in the following illustration:
Wrong figure! Text with no specified style in the document. JUnit Test Framework class structure
junit.framework.test: Test interface.junit.framework.testcase: Test case classes, test case classes for business classe
JUnit is a regression testing framework (regression testing framework ). JUnit test isProgramStaff testing, the so-called white box testing, because the programmer knows that the testedSoftwareHow to complete the function and what the function is. JUnit is a framework that inherits the testcase class and can be automatically tested using
report to you that the reason for the function to end is timeout, in this way, you can find these bugs. To implement this function, you only need to add a parameter to @ test. The Code is as follows:@ Test (timeout = 1000)Public void squareroot ()... {calculator. squareroot (4 );Assertequals (2, Calculator. getresult ());}The timeout parameter table shows the time you want to set, in milliseconds. Therefore, 1000 represents 1 second.
Ii. Test exceptions
Exception Handling in Java is also the fo
and observe the results again. Some people think this is nothing. What if there are 10 test classes? Therefore, the concept of packaging testing is introduced. We can use the mechanism provided by JUnit to execute the previous two test classes at a time.
Right-click a new JUnit test case in the calculator class, select none of the methods, and click Finish. Change the code to the following:
1 package org.
void Add () {System.out.println ("add");int x = 0;int y = 0;addoperation instance = new Addoperation ();int expresult = 0;int result = Instance.add (x, y);Assertequals (Expresult, result);}}We can see that the adoption of annotation JUnit has not been overbearing enough to require you to inherit from the TestCase, and the test method does not have to start with test, as long as the @test metadata to describe it.As you can see from the example above,
JUnit Unit TestAndroid Unit Test base is also relatively simple, but also the test Add method is correct, test case or with a? = 2 A few simple Android unit tests;1. Open Eclipse and create a new project. Click on the menu bar file-"new-" Java project;project name is testjunit;2. Right-click on the newly created Project Testjunit, select New---Java class, create a Java category; class name is addclass;The Java program to be tested is as follows Public
"Turn" junit a dangerous note oneJUnit is a regression testing framework (regression testing framework). JUnit testing is a programmer's test, the so-called white-box test, because the programmer knows how the software being tested (how) accomplishes the function and what it does. JUnit is a set of frameworks that inherit the TestCase class and can be automatical
spring in the JUnit test class.Second, spring configuration file under Web-infOf course, in the development of the Java EE, some people are used to put spring files in the Web-inf directory (although more people are accustomed to put under the classpath), or some spring configuration files are placed under the classpath, and some are placed in theUnder the Web-inf directory, such as.At this point, you cann
In. NET learning, when exposed to NUnit, to Java Learning, of course, the study of JUnit, this blog brief introduction of JUnit's use.1. Right-click-->build path-->add Libraries in the project root directoryFigure 1 Adding a jar package2. Select JUnit under the Add Library and NEXT-->JUNIT4. After selecting Finish, you will find
course, if you need to test both of these two test classes together, it can be implemented by the Testsuite class, which is equivalent to a suite that can add all the test classes and run tests together.The code is as follows:[Java]View Plaincopy
Package com.phicomme.test;
Import com.phicomme.hu.StudentTest02;
Import Junit.framework.Test;
Import Junit.framework.TestSuite;
Public class AllTest
{
//static persontest p = new
()
{
Assertequals ("Hdy", Teststudent.getname ());
}
}
Of course, if you need to test both of these two test classes together, it can be implemented by the Testsuite class, which is equivalent to a suite that can add all the test classes and run tests together.The code is as follows:[Java]View PlainCopy
Package com.phicomme.test;
Import com.phicomme.hu.StudentTest02;
Import Junit.framework.Test;
Import Junit.framework.TestS
In some cases, we need to unit test our own code (the benefit is to reduce the effort and expense of late maintenance), which is some of the most basic module tests. Of course, in the case of unit testing, it is necessary to understand the internal logic implementation of the code we test, so that we can clearly compare the results we expect from the code logic implementation to the actual results of the test when testing.Nonsense less say, on the cod
, @After is @test method run after the operation, looks a bit like an AOP annotation, hehe.Ready to work: Import the JUnit 4 jar package, when running, right-click, run as, select "JUnit Test".Limitations: No action is provided with parameter execution (the new version has wood), of course, you can use @before to do some initialization work, where we can initiali
configuration file under Web-inf
Of course, when doing Java EE development, some people are accustomed to put the spring file in the Web-inf directory (although more people are accustomed to put under the classpath), or some spring configuration files are placed under the classpath, and some are placed in the
Web-inf directory below, as shown below.
At this point, you cannot use Classpathxmlapplicationcontext to load the configuration file in your
)Assertequals ("Nanchang University", Teststudent.getschool ()); } Public voidTestgetname () {assertequals ("Little Flower", Teststudent.getname ()); }}StudentTest02Of course, if you need to test both of these two test classes together, it can be implemented by the Testsuite class, which is equivalent to a suite that can add all the test classes and run tests together.Comprehensive test class: Packageunittestexample;Importunittest.student;Importju
Spring Boot Junit unit test
The old Junit technology is nothing more than to mention, to some extent, to illustrate its importance in the project.Based on my own feelings and experience, the Junit Use Cases Written completely in the project to cover most of the Business Code should not exceed half.
I wrote some posts about SpringBoot some time ago. Now I just wan
I. Introduction
JUnit is an open-source Java unit testing framework. Developed by Erich Gamma and Kent Beck in 1997. Erich Gamma is one of the two cattle, and Kent Beck has made significant contributions to XP (you don't think it's strange to be familiar with it ).
As the saying goes: "The Sparrow is small and dirty ." JUnit is designed to be very small but powerful.
The following is a summary of some fe
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.