`

JUnit In Action读书笔记(1)

阅读更多

JUnit In Action读书笔记(1)

Part1 JUnit distilled

chapter1 will teach you what the JUnit framework is and what problems it solves.
chapter2 will take you on a discovery tour of the core JUnit classes and how to best use them.
chapter3,you'll practice your new JUnit knowledge on a real-world example.
chapter4 steps back and explains why unit test are important and how they fit in the global testing            ecosystem.There,the Test-Driven Developement methodology and provides guidance on measuring        your test coverage.
chapter5 demonstrates how to automatice unit testing using three popular tools:Eclipse,Ant,and maven.

Chapter 1 JUnit jumpstart

we code,compile,run,test again.
Most of us will quickly develope a pattern for our informal tests: We add a record, view a record, edit a record,and delete a record.Running a little test suite like this by hand is easy enough to do;so we do it.Over and over again.

1.1 Proving it works
    Here's a generic description of a typical unit test from our perspective:"Confirm that the method accepts the expected range of input,and that the method returns the expected value for each test input".

    In this chapter, we'll walk through creating a unit test for a simple class from scratch. We'll start by writing some tests manually, so you can see how we used to do things. Then, we will roll out JUnit to show you how the right tools can make life much simpler.

1.2 Starting from scratch

    public class Calculator{
        public double add(double number1, double number2){
            return number1 + number2;
        }
    }

    Yet testing anything at this point seems problematic.You dont even have a user interface with which to enter a pair of doubles. You could write a small commandline program tha waited for you to type in two double values and then displayed the result.Of course, then you would also be testing your own ability to type a number and add the result ourselves. This is much more than you want to do.You just want to know if this "unit of work" will actually add two doubles and return the correct sum.You don't necessarily want to test whether programmers can type numbers!

    You could also run the program again later to be sure the method continues to work as the application grows.

    So what's the simplest possible test program you could write?How about the simple TestCalculator program shown in listing below?

    public class TestCalculator{
        public static void main(String[] args){
            Calculator c = new Calculator();
            double result = c.add(10,50);
            if(result != 60){
                System.out.println("Bad result: "+result);
            }
        }
    }

    But what happens if you change the code so that it fails?(这个changed是怎么change?改哪?这个fails是指哪?是指TestCalculator?还是哪个Test类?).You will have to carefully watch the screen for the error message.(噢,现在明白了些,是说若失败后的有什么信息来告诉你失败了).You may not have to supply the input,but you are still testing your own ability to monitor the program's output.You want to test the code,not yourself!

    The conventional way to handle error conditions in Java is to throw an exception.Since failing the test is an error condition,let's try throwing an exception instead.

    Meanwhile,you may also want to run tests for other Calculator methods that you haven't written yet,like subtract or multiply. Moving to a more modular design would make it easier to trap(这个trap是trap啥的?怎么trap呢?是catch吧?) and handle exceptions and make it easier to extend the test prgram later. the following shows a slightly better TestCalculator program.

    public class TestCalculator{
        private int nbErrors =0;
        public void testAdd(){
            Calculator c = new Calculator();
            double r = c.add(10,50);
            if(r != 60){
                throw new RuntimeException("Bad result: "+ result);
            }
        }

        public static void main(String[] args){
            TestCalculator t = new TestCalculator();

            try{
                t.testAdd();
            }catch(Throwable e){
                t.nbError ++ ;
                e.printStackTrace();
            }

            if(t.nbErrors > 0){
                throw new RuntimeException("There were "+t.nbErrors + "error(s)");
            }
        }
    }

1.3 Understanding unit testing frameworks
    these seemingly minor improvements in the TestCalculator program highlight three rules that (in our experience) all unit testing frameworks should observe:
        Each unit test must run independently of all other unit tests.(这就是说add和multiply得分开吧)
        Errors must be detected and reported test by test.(这个没什么理解上的困难)
        It must be easy to define which unit tests will run.(这个怎么来决定?怎么叫哪一个tests来运行呢?)

    The "slightly better" test program comes close to following these rules but still falls short. For example, in order for each unit test to be truly independent, each should run in different classloader instance.(为什么说要在不同的classloader instance里呢?这个"each"是指TestCalculator呢还是那个Calculator类呢?难道说Calculator这个类在同一个Classloader instance就不能independent?而在多个Classloader instance里又怎么可以independent呢?)

    Adding a class is also only slightly better.

    The most obvious problem is that large try/catch blocks are known to be maintenance nightmares.You could easily leave a unit test out and never know it was't running!(这个leave a unit test out是什么意思?噢,明白了,这句话的意思是说"你很容易就会遗嘱一项单元测试,而你甚至从来都没有意识到这项测试被漏掉了!")

    It would be nice if you could just add new test methods and be done with it.But how would the problem know which methods to run?

    -->a simple registration procedure.A registration method would at least inventory which tests are running.("你应该有一个简单的注册过程,至少会有一个注册方法来盘点要运行哪些测试.")
    
    Another approach would be to use Java's reflection and introspection capabilites.A problem could look at itself and decide to run whatever methods are named in a certain way -- like those that begin with the letters test, for example.

    Happily, the JUnit team has saved you the trouble. The JUnit framework already supports registering or introspecting methods. Is also supports using a different classloader instance(这个classloader instance很有必要么?还是不理解.........) for each test, and reports all errors on a case-by-case basis.

1
1
分享到:
评论

相关推荐

    Junit In Action 学习笔记----下载不扣分,回帖加1分,童叟无欺,欢迎下载

    第1章:带着你为一个简单的对象创建测试。在此过程中介绍了单元测试的好处,理念,和方法。 随着测试越来越复杂,我们把用junit创建更好的测试方案来展现。 第2章:进一步深入研究了Junit的类,生命周期和框架。...

    struts2自学笔记

    Struts2提供了JUnit测试支持,可以通过模拟HTTP请求来测试Action,确保业务逻辑的正确性。 在学习Struts2的过程中,配合源码阅读能够更深入理解其内部工作机制。Struts2的工具如IDEA的插件可以帮助开发者快速创建...

    struts2学习笔记

    1. **Struts2核心概念** - **Action**:是Struts2的核心,负责处理用户的请求,执行业务逻辑,并返回结果。 - **Action Mapping**:定义了Action与URL之间的映射关系,控制请求的流向。 - **Result**:Action执行...

    Struts2 笔记代码

    - **测试类**:可能包含JUnit测试,用于验证Action和拦截器的正确性。 - **日志和异常处理**:如何在Struts2中配置日志框架(如Log4j),以及如何处理应用程序中的异常。 通过分析和实践这些代码,你可以深入理解...

    struts2的学习笔记+测试源代码

    Struts2提供Action测试工具,可以通过JUnit进行单元测试,确保Action的正确性。此外,Struts2的调试也很便捷,通过配置可以输出详细的日志信息,便于问题定位。 八、安全方面 尽管Struts2强大且易用,但历史上曾...

    SSH商城项目笔记.rar

    SSH商城项目笔记主要涵盖了基于Struts2、Spring和Hibernate这三个框架构建电子商务平台的相关技术与实践。这是一套经典的Java Web开发技术组合,通常被称为SSH框架。在这个项目中,你将学习到如何运用这些技术来实现...

    北京圣思园张龙老师Struts2全部课堂笔记

    1. **Struts2架构**:Struts2的核心是Action类,它是业务逻辑处理的中心。通过配置文件或注解,我们可以定义Action与URL的映射关系,实现请求的分发。 2. **拦截器(Interceptors)**:Struts2引入了拦截器机制,...

    struts笔记.rar

    Struts2提供了JUnit测试支持,使得单元测试Action变得更加简单。 以上是关于Struts2.0的一些主要知识点。通过深入学习和实践,开发者可以有效地利用Struts2构建高效、可维护的Java Web应用。希望这些内容对你的...

    SSH笔记_ModelDriven_Prepareable

    1. 源代码:展示了如何在Struts2 Action类中实现`ModelDriven`和`Prepareable`接口的示例。这可能包括Action类、对应的Action配置(例如struts.xml)、以及相关的JSP页面。 2. 数据库配置:可能包含数据库连接信息和...

    Strtus2学习笔记

    ### Struts2学习笔记知识点梳理 #### 一、前言及背景 - **Struts2简介**:Struts2是一个基于MVC模式的开源Web应用框架,它继承了Struts1的一些特性,并在此基础上进行了很多改进,使得开发更加便捷高效。 - **学习...

    webwork学习笔记

    ### WebWork学习笔记知识点 #### 一、WebWork框架简介 - **定义**: WebWork是一个由OpenSymphony组织开发的MVC(Model-View-Controller)框架,专注于组件化和代码重用,适用于J2EE应用程序开发。 - **最新版本**:...

    SSH整合项目及笔记.rar

    1. **配置环境**:确保系统安装了JDK,设置好环境变量,同时还需要安装并配置Tomcat服务器作为运行环境。 2. **导入库**:在项目中引入Struts、Spring和Hibernate的JAR包,以及相关的依赖库,如数据库驱动。 3. **...

    java基础笔记

    1. **Java基础** Java是一种面向对象的、跨平台的编程语言,由Sun Microsystems(现为Oracle公司)于1995年推出。其设计目标是具有简单性、健壮性、安全性、可移植性等特点。Java基础包括以下几个关键部分: - **...

    java笔记 资料收藏大师

    Struts1的核心组件包括Action、ActionForm、DispatcherServlet(Tiles)、配置文件(struts-config.xml)等。理解Struts2的模型驱动、拦截器、OGNL表达式语言以及Action与Result的概念也是很重要的。 3. **SSH整合**...

    Struts2笔记

    11. **测试支持**:Struts2的Action类可以通过JUnit进行单元测试,方便进行功能验证和性能优化。 12. **工具集**:Struts2提供了丰富的工具类库,如日期时间处理、字符串操作、对象复制等,便于开发者进行日常开发...

    JAVA笔记JAVA笔记.docx

    ### JAVA笔记要点解析 #### JAVASE概述与基础概念 - **数组与数组拷贝**: - 数组是固定大小的数据结构,用于存储同种类型的数据元素。 - `java.lang.System`类中提供了静态方法`arraycopy`用于数组拷贝。此方法...

    struts2笔记

    12. **测试支持**:Struts2支持单元测试和集成测试,可以使用JUnit和Mockito等工具对Action进行测试。 总之,Struts2作为一个成熟的Java web框架,提供了全面的功能和高度的可扩展性,帮助开发者构建健壮的MVC应用...

    struts2学习笔记十八(第18讲.Struts2深入探索)

    这篇学习笔记将主要围绕以下几个核心知识点展开: 1. **拦截器(Interceptors)**: Struts2的核心特性之一是拦截器,它允许开发者定义一系列的处理逻辑,这些逻辑会在Action执行前后被调用。拦截器可以用来实现如...

Global site tag (gtag.js) - Google Analytics