Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JUnit4 for unit testing in Eclipse

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要为大家展示了"在Eclipse中如何使用JUnit4进行单元测试",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"在Eclipse中如何使用JUnit4进行单元测试"这篇文章吧。

首先新建一个项目叫JUnit_Test,我们编写一个Calculator类,这是一个能够简单实现加减乘除、平方、开方的计算器类,然后对这些功能进行单元测试。这个类并不是很***,我们故意保留了一些Bug用于演示,这些Bug在注释中都有说明。该类代码如下:

package andycpp;

public class Calculator ...{

private static int result; // 静态变量,用于存储运行结果

public void add(int n) ...{

result = result + n;

}

public void substract(int n) ...{

result = result - 1; //Bug: 正确的应该是 result =result-n

}

public void multiply(int n) ...{

} // 此方法尚未写好

public void divide(int n) ...{

result = result / n;

}

public void square(int n) ...{

result = n * n;

}

public void squareRoot(int n) ...{

for (; ;) ; //Bug : 死循环

}

public void clear() ...{ // 将结果清零

result = 0;

}

public int getResult() ...{

return result;

}

}

第二步,将JUnit4单元测试包引入这个项目:在该项目上点右键,点"属性",如图:

在弹出的属性窗口中,首先在左边选择"Java Build Path",然后到右上选择"Libraries"标签,之后在最右边点击"Add Library…"按钮,如下图所示:

然后在新弹出的对话框中选择JUnit4并点击确定,如上图所示,JUnit4软件包就被包含进我们这个项目了。

第三步,生成JUnit测试框架:在Eclipse的Package Explorer中用右键点击该类弹出菜单,选择"New à JUnit Test Case"。如下图所示:

在弹出的对话框中,进行相应的选择,如下图所示:

点击"下一步"后,系统会自动列出你这个类中包含的方法,选择你要进行测试的方法。此例中,我们仅对"加、减、乘、除"四个方法进行测试。如下图所示:

之后系统会自动生成一个新类CalculatorTest,里面包含一些空的测试用例。你只需要将这些测试用例稍作修改即可使用。完整的CalculatorTest代码如下:

package andycpp;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Ignore;

import org.junit.Test;

public class CalculatorTest ...{

private static Calculator calculator = new Calculator();

@Before

public void setUp() throws Exception ...{

calculator.clear();

}

@Test

public void testAdd() ...{

calculator.add(2);

calculator.add(3);

assertEquals(5, calculator.getResult());

}

@Test

public void testSubstract() ...{

calculator.add(10);

calculator.substract(2);

assertEquals(8, calculator.getResult());

}

@Ignore("Multiply() Not yet implemented")

@Test

public void testMultiply() ...{

}

@Test

public void testDivide() ...{

calculator.add(8);

calculator.divide(2);

assertEquals(4, calculator.getResult());

}

}

第四步,运行测试代码:按照上述代码修改完毕后,我们在CalculatorTest类上点右键,选择"Run As à JUnit Test"来运行我们的测试

以上是"在Eclipse中如何使用JUnit4进行单元测试"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report