Home > Java > javaTutorial > The simplest use of java test unit JUnit

The simplest use of java test unit JUnit

(*-*)浩
Release: 2019-11-27 13:44:24
Original
2087 people have browsed it

The simplest use of java test unit JUnit

Just write a small introduction and simply output text. First, create a MessageUtil class for testing (recommended learning: java course)

package com.test.jiao;

public class MessageUtil {

    private String message;

    public MessageUtil(String message){
        this.message = message;
    }

    public String printMessage(){
        System.out.println(message);
        return message;
    }
}
Copy after login

Then, create a TestCase class, named TestJunit, and use assertEquals () to execute during testing

package com.test.jiao;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class TestJunit {

    String message = "Hello Jiao, Junit is working!";
    MessageUtil messageUtil = new MessageUtil(message);

    @Test
        public void testPrintMessage(){
        //Junit 的 assertEquals API 执行测试
        assertEquals(message,messageUtil.printMessage());
        }
}
Copy after login

Finally, create a startup class TestRunner, which uses the runClasses method of JUnit's JUnitCore class to test

package com.test.jiao;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static void main(String[] args){
        //运用JUnit的JUnitCore类的runClasses方法来测试
        Result result = JUnitCore.runClasses(TestJunit.class);
        //获取测试结果
        for(Failure failure : result.getFailures()){
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}
Copy after login

OK, now run TestRunner to see the results (●'◡'●)

The simplest use of java test unit JUnit

Success!

The above is the detailed content of The simplest use of java test unit JUnit. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template