`

Use JUnit

    博客分类:
  • Java
阅读更多

Junit  A programmer-oriented testing framework for Java

Simple Test Case[1]How do you write testing code?

The simplest way is as an expression in a debugger. You can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects. You can also write test expressions as statements which print to the standard output stream. Both styles of tests are limited because they require human judgment to analyze their results. Also, they don't compose nicely- you can only execute one debug expression at a time and a program with too many print statements causes the dreaded "Scroll Blindness".

JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time. When you need to test something, here is what you do:

  1. Annotate a method with @org.junit.Test
  2. When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds
For example, to test that the sum of two Moneys with the same currency contains a value which is the sum of the values of the two Moneys, write:
@Test public void simpleAdd() {     
	Money m12CHF= new Money(12, "CHF");      
	Money m14CHF= new Money(14, "CHF");      
	Money expected= new Money(26, "CHF");      
	Money result= m12CHF.add(m14CHF);      
	assertTrue(expected.equals(result)); 
}
 
If you want to write a test similar to one you have already written, write a Fixture instead.


Create a new project de.vogella.junit.first. We want to create the unit tests in a separate folder. The creation of a separate folder for tests is not mandatory. But it is a good practice to keep the code separated from the regular code. You might even create a separate project for the test classes, but we skip this step to make this example simpler.[2]


分享到:
评论

相关推荐

    Pragmatic Unit Testing in Java 8 with JUnit(PACKT,2015)

    Once past the basics, you'll learn why you want to write unit tests and how to effectively use JUnit. But the meaty part of the book is its collected unit testing wisdom from people who've been there,...

    How to use the Junit in JDeveloper

    《如何在JDeveloper中使用JUnit》 JUnit是Java编程语言中最常用的单元测试框架,它使得开发者可以方便地创建和执行测试用例,确保代码的质量和稳定性。在JDeveloper这款强大的集成开发环境中,JUnit的集成使用使得...

    JUnit Ant Eclipse, JUnitReport报告

    <formatter type="plain" usefile="false"/> ${build.dir}/test-reports"/> </junit> ``` 这段代码会运行指定的测试类`com.example.YourTestClass`,并将结果打印到控制台。 JUnitReport是Ant的一个插件,用于...

    junit官方jar包

    Unit是由 Erich Gamma 和 ...Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。

    JUnit使用ANT格式化生成html测试报告

    <formatter type="html" usefile="true" /> **/*Test.java"/> </junit> ``` 这段配置会运行`src`目录下所有以`Test`结尾的类中的测试方法,并将结果输出到`test-reports`目录下的HTML文件中。 5. **...

    Ant 使用Junit进行单元测试

    <formatter type="plain" usefile="false"/> <!-- 指定要运行的测试类 --> </junit> ``` 在这个配置中,我们首先定义了项目类路径,包括JUnit库和项目编译后的类文件。然后,我们有两个目标,一个是`...

    ant-junit-1.7.0.jar.zip

    <formatter type="plain" usefile="false"/> </junit> ``` 这段代码定义了一个名为“test”的目标,它将运行名为“com.example.YourTestClass”的测试类,并将结果输出到控制台。 4. ant.license.txt “ant....

    java测试用例JUnit教程

    Java 测试用例 JUnit 教程 Java 是一种广泛使用的编程语言,而 JUnit 是 Java 测试框架中最流行的测试框架之一。JUnit 提供了一组丰富的断言方法,帮助开发者编写可靠的测试用例,从而提高代码质量。本教程旨在...

    maven+springMVC+mybatis+junit整合过程

    在Eclipse中,选择“New” -> “Maven Project”,然后取消勾选“Create a simple project (use archetype)”选项,设置打包类型(Packing)为“war”。这将生成一个基本的Maven项目结构,包含`src/main/java`、`src...

    junit4.12 hamcrest-core-1.3

    在使用junit4.12时,同时需要hamcrest-core-1.3 http://junit.org/junit4/ junit.jar: Includes the Hamcrest classes. The simple all-in-one solution to get ... Lets you use a different Hamcrest version.

    java-junit4.11

    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND ...WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    Springboot使用Junit测试没有插入数据的原因

    6. 测试类所在文件夹必须为源文件夹(source files),如果不是,选择 ‘Build path' -> ‘Use as a source folder' 7. 测试类是否继承 TestCase,如果是,删除继承 8. 在类上执行 Junit run 单元测试时,如果报错:...

    JUnit4和Guice测试库Acai.zip

     // Use the injected value of foo here  }  private static class MyTestModule extends AbstractModule {  @Override protected void configure() {  bind(MyClass.class).to...

    software test automation effective use of test execution 自动化测试

    常见的自动化测试工具有Selenium用于Web应用测试,Appium针对移动应用,JUnit和TestNG是Java环境下的单元测试框架,还有JMeter用于性能测试。选择合适的工具能有效提升测试效率,同时降低学习和维护的成本。 实施...

    Software Test Automation - Effective use of test execution tools

    6. **测试框架**:介绍各种流行的测试框架,如JUnit、Selenium、Appium等,以及如何利用它们来构建和执行自动化测试。 7. **性能与负载测试**:讨论自动化工具在性能测试和负载测试中的应用,以评估系统在高并发或...

    53613290ShlTest_use_源码

    3. **单元测试**:由于有“Test”字样,这可能是一个包含单元测试的项目,涉及到断言、测试框架如JUnit、pytest或Google Test,以及如何编写可测试的代码。 4. **版本控制**:源码通常会用版本控制系统如Git进行...

    openhie-integration-tests:OpenHIE 集成测试作为 JUnit 代码

    openhie-集成-测试OpenHIE 集成测试作为 JUnit 代码配置单元 Tets 单元测试被配置为一系列参数到 JUnit 主机(例如 eclipse 或 Bamboo),它们如下: ohie-cr-endpoint = ipaddress:port = of the client registry ...

Global site tag (gtag.js) - Google Analytics