`
fujohnwang
  • 浏览: 156312 次
社区版块
存档分类
最新评论

Gotchas With JUnit's Execution Model

    博客分类:
  • Tech
阅读更多

Maybe you have known it before, or maybe not, no matter what, It’s a funny topic.
The Code below seems simple, so let’s see what happens with it:

public class MyTest extends TestCase {
    private int count = 0;
    public void test1() {
        count++;
        assertEquals(1, count);
    }
    public void test2() {
        count++;
        assertEquals(1, count);
    }
}

 

If you run this unit test, will it pass or not?

I seldom wrote too many unit tests before unless I feel it’s necessary, so I didn’t notice the problem of JUnit’s execution model, even you have written many unit tests, but don’t write enough functional tests or integration tests, I don’t think you can find the same problem either.

Although JUnit’s designer have their reasons or philosophy, but I do think the unit test and their execution model should be separated and are different things.  The execution model should not influence the unit test definition. But many JUnit’s best practice are advocated with implicit precondition of its execution model. I know someone(usually don’t know what they are doing) prefer to use static fields or methods, but most of the times, it’s a smell, bad smell of the code. Have u thought why you have to use too many static fields or methods, even static blocks? It’s because JUnit’s execution model mandate u to do it. It works, but it doesn’t mean it’s natural , rational or the best.

Until now, I have talked too much that have nothing to do with our unit test code above, OK, I admit I do it on purpose, ‘cause  I want u to find out what’s really going on with it. Now, I tell you that the unit test above will PASS.

If you have been familiar with JUnit’s execution model for a long time, you will think it’s not a big deal, BUT I should say, it make me sick for a while and still be sick with it, that’s why  I have moved all of my unit tests to TestNG. If you can go back without any JUnit framework’s experience, just think about it that if we have a class definition like the unit test above, what’s the result you will expect? I think a natural reaction should be that it should fail. Otherwise, the code makes u feel unnatural, right?

JUnit will create a new instance for each test method when executing the test, most of the time, you may let it go because it doesn’t bother you too much, even it does, you will resort to the best practices of JUnit and get through it. It makes sense as per JUnit’s design philosophy, but it doesn’t make sense to me. It mixes 2 concerns together and make things “dirty”. When you have to setup heavy resources, this model makes things worse, yeah, I know you would suggest to setup such things in static block, but why should I do things this way, is it the best and only way? No, even you have been fucked by GGFFWW for a long time and become to accept it, but it doesn’t mean u are treated fairly and there are no other options.

If you do still want to use it, use it in proper way and avoid getting trapped by gotchas like this one.

分享到:
评论

相关推荐

    [CS]C++ Gotchas

    《C++ Gotchas》是一本专门探讨C++编程中容易犯错或不为人知的细节的书籍,旨在帮助程序员避免常见的编程陷阱。C++作为一门强大的编程语言,其丰富的特性和复杂的规则使得开发者在编写代码时很容易陷入各种“gotchas...

    Gotchas-in the Verilog and SystemVerilog Standards.zip

    讲一些在Verilog和SystemVerilog coding中容易发生的一些错误,以及如何避免。 paper及PPT发表在 SNUG Boston... Dozens of gotchas in the standards are explained, along with tips on how to avoid these gotchas.

    verilog and systemverilog 101 gotchas

    在介绍《Verilog和SystemVerilog 101 Gotchas》这本书的详细知识点之前,首先要说明的是,这本书是由Stuart Sutherland撰写,他在Verilog及SystemVerilog标准的定义中扮演了重要的角色,同时也是IEEE官方Verilog和...

    C.Gotchas (C++ 图书)

    《C++ Gotchas》是一本专门探讨C++编程中容易犯错或不为人知的陷阱的书籍,旨在帮助程序员深入理解C++语言的微妙之处,避免在编程过程中遇到的潜在问题。这本书通常会涵盖一系列常见但又易于忽视的错误,这些错误...

    C++程序设计陷阱(C++Gotchas)

    C++程序设计陷阱(C++Gotchas)

    C++ Gotchas Avoiding Common Problems in Coding and Design epub

    C++ Gotchas Avoiding Common Problems in Coding and Design 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    C++程序设计陷阱(C++Gotchas) 中英两个版本

    《C++程序设计陷阱》是一本深入探讨C++编程中潜在问题的专业书籍,旨在帮助开发者避免常见的编程陷阱和误区。该书分为中英文两个版本,分别以PDF和CHM格式提供,为不同语言习惯的读者提供了便利。...

    [AddisonWesley]C++_Gotchas.zip

    【标题】:“AddisonWesley”出版社的“C++ Gotchas”压缩包 这个压缩文件代表了由著名出版商AddisonWesley发行的一本关于C++编程常见陷阱和问题的资源。"Gotchas"在编程领域通常指的是那些容易被忽视、可能导致...

    C++ Gotchas .chm

    Addison Wesley - C++ Gotchas .chm 好书不用多说~~~

    Thinking in C++ Accelerated C++ C++ Gotchas

    Thinking in C++ Accelerated C++ C++ Gotchas C++初学者50个忠告中提及的资料整理[分10卷下载 每卷50M]\C++初学者50个忠告中提及的资料整理[分10卷下载每卷50M].part01

    vc编程系列之Addison Wesley - C++ Gotchas

    《VC编程系列之Addison Wesley - C++ Gotchas》是一本深入探讨C++编程中常见陷阱和误区的专业书籍。在C++编程过程中,开发者往往容易遇到一些不易察觉的问题,这些问题可能导致程序行为不符合预期,甚至引发严重错误...

    STL Gotchas.pdf

    ### STL Gotchas - 避免在使用STL时常见的错误 #### 一、插入和删除可能使引用、指针和迭代器失效 - **背景**:STL容器是动态大小的,可以根据需要增长。它们提供了成员函数`insert()`和`erase()`来添加和移除元素...

    r-gotchas:陷阱

    一些陷阱R函数有一些潜在的令人惊讶的结果,需要注意。 是一个包含更多内容的stackoverflow讨论。 甚至更多:以及更多:尺寸下降对于矩阵: m <- matrix ( 1 : 6 , ncol = 3 )m ## [,1] [,2] [,3]## [1,] 1 3 5##...

    microsoft.net gotchas

    在.NET开发领域,Microsoft.NET Gotchas是一系列常见的陷阱和误区,开发者可能会遇到这些问题,从而导致程序的不稳定或者性能下降。本文将深入探讨这些.NET开发中的潜在问题,帮助开发者避免不必要的困扰。 1. **...

    Verilog and system verilog

    Verilog and system verilog学习的好资料, Verilog and SystemVerilog Gotchas_101 Common Coding Errors and How to Avoid Them

Global site tag (gtag.js) - Google Analytics