简体   繁体   English

JUnit测试-我要测试什么?

[英]JUnit Tests - what do i test?

If I have a basic accessor method that returns an ArrayList 如果我有一个返回ArrayList的基本访问器方法

What exactly would i test for it? 我到底要测试什么? I am very inexperienced when it comes to testing. 对于测试,我没有经验。

That depends on how you expect the method to behave. 这取决于您期望该方法的行为。 For example: If someone has called the method and changed the list that was retrieved, do you want those changes to show up the next time the getter is called? 例如:如果有人调用了该方法并更改了检索到的列表,您是否希望这些更改在下次调用getter时显示? Either way, test that behaviour. 无论哪种方式,请测试该行为。 What does the getter return when the list would be empty? 当列表为空时,getter返回什么? Null or an empty list? 空列表还是空列表? This should also be tested. 这也应该进行测试。

Interessting question for you: 询问您的问题:

How much should a unit test "test" 单元测试应“测试”多少

How to use Junit and Hibernate usefully 如何有效地使用Junit和Hibernate

What should not be unit tested 什么不应该进行单元测试

Edit: 编辑:

Added some of my favorite Questions here on Stackoverflow regarding JUnit and Unit Testing. 在此处有关Stackoverflow的JUnit和单元测试中添加了一些我最喜欢的问题。

Typically writing explicit Junit tests for accessors is usually a little overkill (what are you testing? return foo;). 通常,为访问器编写明确的Junit测试通常有点过大(您要测试什么?return foo;)。 Using a code coverage tool such as clover can help you target your testing efforts at your most complicated code first. 使用三叶草代码覆盖工具可以帮助您首先将测试工作针对最复杂的代码。

Always keeps in mind that test code is also code and for every 1,000 lines of code, you produce at least 4 bugs. 始终牢记,测试代码也是代码,对于每1,000行代码,您至少会产生4个bug。 So test what doesn't work and don't write tests for something that can't possibly break (like code generated by your IDE). 因此,请测试不起作用的内容,并且不要为可能无法破坏的内容(例如,IDE生成的代码)编写测试。 If it does break, write a test :) 如果确实损坏,请编写测试:)

In general unit tests should test that your method does what it states it should do. 通常,单元测试应该测试您的方法是否执行了其声明应执行的操作。

If your method returns an arraylist your basic test is to assert that an arraylist is indeed returned when it is called. 如果您的方法返回一个arraylist,则您的基本测试是断言一个arraylist在调用时确实返回了。

The next level of detail in the test is to check is the arraylist constructed correctly? 测试中的下一个详细级别是检查arraylist是否正确构建? Have the values you expect in it been filled correctly? 您期望的值是否正确填写? If it's supposed to be an empty list, is that the case? 如果应该是一个空列表,是这样吗?

Now you have your "sunny day" case (ie the method works under normal conditions) you should add some negative (or "rainy day") conditions if appropriate. 现在您有了“晴天”的情况(即该方法在正常条件下有效),您应该在适当的情况下添加一些负面(或“雨天”)条件。 If the method accepts a length for the array, what if you pass in a negative number or a int.Max etc. 如果该方法接受数组的长度,那么如果您传递负数或int.Max等,该怎么办。

As stated in another answer, this is probably overkill for a simple accessor, but the principles apply to any unit tests you need to write. 如另一个答案所述,对于一个简单的访问器来说,这可能是过高的,但是这些原理适用于您需要编写的任何单元测试。

Depends on your requirements. 取决于您的要求。 You might test: 您可以测试:

  1. If the return value is not null 如果返回值不为null
  2. If the returned collection is not empty 如果返回的集合不为空
  3. If the returned collection is modifiable/unmodifiable 如果返回的集合是可修改/不可修改的
  4. If the returned collection is sorted 如果返回的集合已排序
  5. If the returned collection contains all expected values 如果返回的集合包含所有期望值
  6. If the accessor method does not throw a runtime exception 如果accessor方法未引发运行时异常

But, as I said, it depends on the requirements, it depends on the 'kind' of collection you expect when you call the accessor. 但是,正如我所说,这取决于要求,取决于您在调用访问器时期望的集合的“种类”。 Maybe you allow setting the list to null but create an empty list. 也许您可以将列表设置为null但是创建一个空列表。 The the test could make sure that you really get an empty list when you set the list to null. 该测试可以确保将列表设置为null时确实得到一个空列表。

Hope it helps to give you an idea! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM