Java 源代码测试:使用 Eclipse 编译并执行 Printtokens2.java
使用 Eclipse 建立项目并执行 Printtokens2.java 测试
1. 项目建立和执行
- **打开 Eclipse 并创建一个新项目。**选择 File -> New -> Java Project,输入项目名称,例如“Printtokens2Test”。2. **导入 Printtokens2.java 代码。**将代码复制到项目中的 src 目录下。3. **编写 JUnit 测试代码。**在项目中创建测试类,例如 TestPrinttokens2.java,并在其中添加测试用例。4. **执行测试。**右键点击测试类,选择 Run As -> JUnit Test。
2. 白盒测试用例设计
需满足判定+条件覆盖准则,具体设计如下:
| 输入 | 期望输出 ||----------------------|---------------------------------------------|| null | 返回 null || '' | 返回 null || 'and or if' | keyword, 'and'. keyword, 'or'. keyword, 'if'. || '[]() , /' |lsquare. rsquare. lparen. rparen. comma. bquote. divide.||'test1 test2' |identifier, 'test1'. identifier, 'test2'.||'#a' |character, 'a'. ||'123 456' |numeric, 123. numeric, 456. ||'string'`` | string, 'string'. || ; | semicolon. || 'abc;123' | identifier, 'abc'. semicolon. numeric, 123. || 'not_exist_file.txt' | 返回 null |
3. 测试用例具体输入和预期输出
| 输入 | 期望输出 ||----------------------|---------------------------------------------|| null | null || '' | null || 'and or if' | keyword, 'and'. keyword, 'or'. keyword, 'if'. || '[]() , /' |lsquare. rsquare. lparen. rparen. comma. bquote. divide.||'test1 test2' |identifier, 'test1'. identifier, 'test2'.||'#a' |character, 'a'. ||'123 456' |numeric, 123. numeric, 456. ||'string'`` | string, 'string'. || ; | semicolon. || 'abc;123' | identifier, 'abc'. semicolon. numeric, 123. || 'not_exist_file.txt' | null |
4. JUnit 测试代码javapackage JUnit4Printtokens2;
import static org.junit.Assert.assertEquals;import org.junit.Test;import java.io.BufferedReader;import java.io.IOException;import java.io.StringReader;
public class TestPrinttokens2 {
@Test public void testOpenTokenStreamNull() throws IOException { Printtokens2 t = new Printtokens2(); BufferedReader br = t.open_token_stream(null); assertEquals(null, br); }
@Test public void testOpenTokenStreamEmptyString() throws IOException { Printtokens2 t = new Printtokens2(); BufferedReader br = t.open_token_stream(''); assertEquals(null, br); }
@Test public void testGetToken() throws IOException { Printtokens2 t = new Printtokens2(); BufferedReader br = new BufferedReader(new StringReader('and or if')); String tok = t.get_token(br); assertEquals('and', tok); tok = t.get_token(br); assertEquals('or', tok); tok = t.get_token(br); assertEquals('if', tok); }
@Test public void testPrintTokenKeyword() { Printtokens2 t = new Printtokens2(); t.print_token('and'); assertEquals('keyword,'and'.
', systemOut()); }
@Test public void testPrintTokenSpecSymbol() { Printtokens2 t = new Printtokens2(); t.print_token('('); assertEquals('lparen.
', systemOut()); }
@Test public void testPrintTokenIdentifier() { Printtokens2 t = new Printtokens2(); t.print_token('test1'); assertEquals('identifier,'test1'.
', systemOut()); }
@Test public void testPrintTokenNumConstant() { Printtokens2 t = new Printtokens2(); t.print_token('123'); assertEquals('numeric,123.
', systemOut()); }
@Test public void testPrintTokenCharConstant() { Printtokens2 t = new Printtokens2(); t.print_token('#a'); assertEquals('character,'a'.
', systemOut()); }
@Test public void testPrintTokenStrConstant() { Printtokens2 t = new Printtokens2(); t.print_token('string'); assertEquals('string,'string'.
', systemOut()); }
@Test public void testPrintTokenSemicolon() { Printtokens2 t = new Printtokens2(); t.print_token(';'); assertEquals('semicolon.
', systemOut()); }
// ... 其他测试用例
private String systemOut() { // ... 获取 System.out 内容
原文地址: https://www.cveoy.top/t/topic/nnrV 著作权归作者所有。请勿转载和采集!