Junit 单元测试

本文详细介绍了如何使用Junit进行单元测试,包括测试public方法和void方法的实践技巧,并提供了一个具体的待测代码示例,帮助开发者更好地理解和应用单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 测试public方法

待测代码:

package myPro;

public class ACase {
	public String echoRequest(String[] request) {
		StringBuffer sb = new StringBuffer();
		for(String str: request) sb.append(" " + str);
        return "Hello!" + sb.toString();  
    }  
      
    public String echoRequest() {  
        return "Hello!";  
    }
}
测试代码:

package myPro;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class ACaseTest {
	ACase a = null;
	
	@Before
	public void setUp() throws Exception {
		a = new ACase();
	}

	@Test
	public void testEchoRequestStringArray() {
		String[] request = "this is a test information".split(" ");
		String s = a.echoRequest(request);
		System.out.println(s); 
	    assertNotNull(s);
	}

	@Test
	public void testEchoRequest() {
		String s = a.echoRequest();
		System.out.println(s); 
		assertNotNull(s);
	}
}

2. 测试staic内部类的private方法

待测代码:

package myPro;

public class BCase {
	static class InnerBCase {
		private String echoRequest(String[] request) {
			StringBuffer sb = new StringBuffer();
			for(String str: request) sb.append(" " + str);
			return "Hello!" + sb.toString();  
		}  
      
		private String echoRequest() {  
			return "Hello!";  
		}
	}
}

测试代码:

package myPro;

import static org.junit.Assert.*;

import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;

public class BCaseTest {
	BCase.InnerBCase a = null;

	@Before
	public void setUp() throws Exception {
		a = new BCase.InnerBCase();
	}
	
	/**
	 * @function: test method with parameters
	 */
	@Test  
	public void testParamEchoRequest() throws Exception {
		Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String[].class);   
		testNoParamMethod.setAccessible(true);
		
		String[] strs = "this is a test information".split(" ");
	    Object result = testNoParamMethod.invoke(a, (Object)strs); //注意此处String[]类型的参数,必须加上(Object),否则会认为为可变参数,报错
	    System.out.println(result);
	    assertNotNull(result);  
	}
	
	/**
	 * @function: test methods with no parameters
	 */
	@Test  
	public void testNoParamEchoRequest() throws Exception {
		Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null);
	    testNoParamMethod.setAccessible(true);  
	 
	    Object result = testNoParamMethod.invoke(a, null);  
	    System.out.println(result); 
	    assertNotNull(result);
	}  
}

3. 测试void方法

待测代码:

package myPro;

public class CCase {
	String nickname = null;
	String[] logFormat = null;
	
	private void generateLogFormat(String str) {
		if (str == null)
			return;

		str = str.trim();
		int k = str.length() - 1;
		while (k>=0 && str.charAt(k) != '"')
			k--;
		if(k <= 1) return;
		
		nickname = str.substring(k + 1).trim();
		str = str.substring(1, k);
		logFormat = str.split(" ");
	}
}
测试代码:

package myPro;

import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;

public class CCaseTest {
	CCase c = null;
	
	@Before
	public void setUp() throws Exception {
		c = new CCase();
	}
	
	@Test
	public void testGenerateLogFormat() throws Exception {
		String str = "\"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"\" combined";
		
		Method testNoParamMethod = c.getClass().getDeclaredMethod("generateLogFormat",String.class);   
		testNoParamMethod.setAccessible(true);
		
		testNoParamMethod.invoke(c, (Object)str);
		
		assertNotNull(c.nickname);
		System.out.println(c.nickname);
		assertNotNull(c.logFormat);
		for(String s: c.logFormat) System.out.print(s + " ");
		System.out.println();
	}
}

推荐:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值