Home Java javaTutorial Graphical Java Junit entry case (code)

Graphical Java Junit entry case (code)

Mar 12, 2017 am 09:29 AM

Detailed explanation of Java's Junit entry case (code)

Go directly to the code demonstration:

For example, you have written an ArrayList Class method:

public class ArrayList implements List { 
    private int size = 0;
    
    private Object[] elementData = new Object[100];
    
    public void add(Object o){
        
    }
    public void add(int index, Object o){
        
    }
    
    public Object get(int index){
        return Object;
    }
    
    public Object remove(int index){
        return Object;
    }
    
    public int size(){
        return -1;
    }  
}
Copy after login

What should I do if I want to test it now? At this time, the Junit tool can be used;

First we create a Junit class:

Graphical Java Junit entry case (code)

If you right-click directly on the class you want to test You can directly check the required test methods, such as:

Graphical Java Junit entry case (code)

Then you can start adding methods directly:

public class ArrayListTest {

   // 这里的@Test是必须的注释,就是告诉JUnit这里就是一个测试方法
    @Test
    public void testGet() {
        static Object[] Data = new Object[]{1,2,3,4,5,6,7,8};
            ArrayList test;
                // 添加数据到test中
                .........
                ......... 

                //测试test 这里要说一下,
                //出了要assertEqual 判断值是否相等的话,其实还有assertFalse,assertNull等方法判断
                assertEqual(Data[1],test.get(1));
    }    
}
Copy after login

Yes, that’s right, it’s that simple. If there is no problem with your class, you should be able to return a picture like this:

Graphical Java Junit entry case (code)

However, have you found that if you are testing other methods, such as add and remove, It is necessary to have an ArrayList pre-populated with data. Do I need to do this every time I run a test?

This is too redundant. At this time, JUnit has its own clever trick:

public class ArrayListTest {

    static Object[] Data = new Object[]{1,2,3,4,5,6,7,8};
    ArrayList test;
       
    //这个before注释可以理解成:
    //在执行每个@Test修饰的方法前都先要执行这个setUp,等于前置条件一样
    @Before
    public void setUp() throws Exception{
        test = new ArrayList();
        for(Object data: Data){
            test.add(data);
        }
    }
        //test function ;

}
Copy after login

It seems to be almost OK now, but what should I do when there are multiple test classes, run them one by one? Isn't this similar to creating a main method test?

So Junit provides another test suite group:

//你现在有3个测试类
public class Test1{
    @Test
    public void test(){
        //...Test1
    }
}

public class Test2{
    @Test
    public void test(){
        //...Test2
    }
}
public class Test3{
    @Test
    public void test(){
        //...Test3
    }
}


//创建一个测试套件类(测试套件可以互相叠加的):
@RunWith(Suite.class)
@Suite.SuiteClasses({test1.class,test2.class,test3.class
                    })
public class SuitTest {
  //必须是public 修饰的,空类
}
Copy after login

In addition to the comments written above, there are actually quite a few, but I won’t go into them yet. (You are too scumbag to use these, run away...), I will post it for readers to take a look at:

Graphical Java Junit entry case (code)

That’s all, it’s just an introduction That’s all. There is no advanced technology or thinking. simple: -/.

I would like to share a sentence at the end:

Test cases are used to prove that you are not wrong, not to prove that you are right.

I personally feel that these words really speak to my heart.


The above is the detailed content of Graphical Java Junit entry case (code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles