java中使用junit测试

时间:2022-05-04
本文章向大家介绍java中使用junit测试,主要内容包括1.准备、2.编写功能代码、3.编写测试用例、4.使用注解、5.注意、6.运行maven的test或install自动执行测试、7.Assert、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

最初写代码只要功能走通就不管了,然后如果出了什么问题再去修改,这是因为没做测试的工作。测试其实很简单。

1.准备

当前使用idea编写代码,用maven构建工程,使用maven的test功能来进行批量测试。测试工具为junit。

2.编写功能代码

将主要的业务功能的代码完成。

 1 public interface Sender {
 2     public void send();
 3 }
 4 public class MailSender implements Sender {
 5     @Override
 6     public void send() {
 7         System.out.println("This is emailSender!");
 8     }
 9 }
10 public class SmsSender implements Sender {
11     @Override
12     public void send() {
13         System.out.println("This is SmsSender!!");
14     }
15 }
16 public class SendFactory {
17 
18     public Sender produce(String type){
19         if("email".equals(type)){
20             return new MailSender();
21         }
22         if ("sms".equals(type)){
23             return new SmsSender();
24         }
25         System.out.println("输入类型不正确!");
26         return null;
27     }
28 }

3.编写测试用例

在需要测试的类里,按快捷键:ctrl+shif+t,创建一个test用例,可以选择要测试的方法。

4.使用注解

测试的功能模块一般都会有准备环境和结束行为。在junit里,使用注解@Before,@After编写前后的代码,@Test里面就是测试内容。

 1 package com.test.java.designPattern.factory;
 2 
 3 import junit.framework.TestResult;
 4 import junit.framework.TestSuite;
 5 import org.junit.After;
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 import static org.junit.Assert.*;
10 
11 /**
12  * Created by mrf on 2016/2/25.
13  */
14 public class SendFactoryTest {
15 
16     protected long startTime;
17     protected long endTime;
18 
19     @Before
20     public void setUp() throws Exception {
21         this.startTime= System.currentTimeMillis();
22         System.out.println("=========开始测试===========");
23     }
24 
25     @After
26     public void tearDown() throws Exception {
27         this.endTime = System.currentTimeMillis();
28         System.out.println("测试用时:"+(endTime-startTime));
29         System.out.println("=========测试结束===========");
30     }
31 
32     @Test
33     public void testProduce() throws Exception {
34         SendFactory sendFactory = new SendFactory();
35         Sender sender = sendFactory.produce("email");
36         sender.send();
37     }
38 
39     @Test
40     public void testM(){
41         System.out.println(12);
42     }
43     
44 }

5.注意

maven测试一般文件位于test/java下对应的包下的的测试类,类名为要测试的类名+Test,要测试的方法为test+要测试的方法名。如上。

6.运行maven的test或install自动执行测试

 可以直接在方法名上右键运行,也可以在maven中test或install。

如果光标位于方法体内,右键会出现运行这个测试方法,将光标移出方法,右键直接运行test用例,会运行所有@Test注解下的方法。

maven的test或install则直接测试所有的方法。

1 =========开始测试===========
2 Disconnected from the target VM, address: '127.0.0.1:6678', transport: 'socket'
3 This is emailSender!
4 测试用时:2
5 =========测试结束===========
6 =========开始测试===========
7 12
8 测试用时:0
9 =========测试结束===========

7.Assert

Junit4提供了一个Assert类(虽然package不同,但是大致差不多)。Assert类中定义了很多静态方法来进行断言。列表如下:

  • assertTrue(String message, boolean condition) 要求condition == true
  • assertFalse(String message, boolean condition) 要求condition == false
  • fail(String message) 必然失败,同样要求代码不可达
  • assertEquals(String message, XXX expected,XXX actual) 要求expected.equals(actual)
  • assertArrayEquals(String message, XXX[] expecteds,XXX [] actuals) 要求expected.equalsArray(actual)
  • assertNotNull(String message, Object object) 要求object!=null
  • assertNull(String message, Object object) 要求object==null
  • assertSame(String message, Object expected, Object actual) 要求expected == actual
  • assertNotSame(String message, Object unexpected,Object actual) 要求expected != actual
  • assertThat(String reason, T actual, Matcher matcher) 要求matcher.matches(actual) == true