Home » Tag Archives: JUnit (page 4)

Tag Archives: JUnit

Test Doubles: mocks, dummies and stubs

Most classes have collaborators. When unit testing, you usually want to avoid using real implementations of those collaborators to avoid test brittleness and binding/coupling, and instead use Test Doubles: Mocks, Stubs and Doubles. This article references two existing articles on the subject: Mocks Aren’t Stubs, by Martin Fowler and The Little Mocker, by “Uncle” Bob Martin. I recommend them both. Terminology I am going to ...

Read More »

Clean Integration Testing with JUnit Rules

java-interview-questions-answers

The benefits of JUnit Rules, especially when dealing with integration testing, can hardly be overrated. In this post, we’ll shine a light on the usefulness of ExternalResource extensions. These simplify fixture control in cases where we have to work with external-resource-abstracting third-party libraries. As an example, we’ll have a look at how to verify the correct retrieval of a list ...

Read More »

Parameterized integration tests with Spring JUnit Rules

Spring 4.2 comes with brand new JUnit rules: SpringClassRule and SpringMethodRule. The main advantage of using JUnit rules is to let developers get rid of SpringJUnit4ClassRunner and utilize different JUnit runners in Spring integration tests. I think the biggest opportunity with Spring JUnit Rules is the ease of creating parameterized integration tests. The code to be tested For the purpose ...

Read More »

Adding Database Logging to JUnit3

We have written many thousands of JUnit3 tests over the last decade and are now trying to consolidate the results in a database instead of scattered log files. It turns out to be remarkably easy to extend the TestCase class to do this. Note: this approach does not directly apply to JUnit4 or other test frameworks but it’s usually possible ...

Read More »

Using Junit Test Name

Name your tests When we create Junit test usually there is no practical use of the name of the method. The Junit runner uses reflection to discover the test methods and since version 4 you are not restricted to start the name of the method with test anymore. The name of the test methods are there for documentation purpose. There ...

Read More »

JUnit: Testing Exceptions with Java 8 and AssertJ 3.0.0

AssertJ 3.0.0 release for Java 8 makes testing exceptions much easier than before. In one of my previous blog post I described how to utilize plain Java 8 to achieve this, but with AssertJ 3.0.0 much of the code I created may be removed. Warning: this blog post contains mostly the code examples.     SUT – System Under Test ...

Read More »

Automated testing of REST-services

Despite I’m a Java and Scala developer I still passioned a lot in testing of software. If to be more precisely – web applications. It’s really interesting to develop web app and be confident that the apps have a good quality. When I’ve started a career the most popular web architecture was MVC (Model View Control) and there was pretty ...

Read More »

Nifty JUnit : Using Rule on Method and Class level

As shown in a the post Nifty JUnit : Working with temporary files, it is possible to use @Rule in a JUnit test, which is a Method level Rule. In this example I would like to show the variation of the @ClassRule for a Class level Rule. Method Rule The @Rule is fired before each test method (just like @Before) ...

Read More »

单元测试Junit入门(PDF下载)

编者按:在Java Code Geeks,我们已经提供了大量JUnit的教程,例如JUnit入门示例,使用JUnit的断言和注释例子,JUnit的注释示例等。然而,我们倾向收集所有的JUnit特性在一个详细的指南以方便读者。我们希望你喜欢它! 目录 1.单元测试的介绍 1.1. 什么是单元测试? 1.2. 测试覆盖 1.3. 在Java中的单元测试 2. JUnit的介绍 2.1. 使用Eclipse JUnit的简单示例 2.2. JUnit的注释 2.3. JUnit断言 3. JUnit的使用Eclipse完整的例子 3.1. 初始步骤 3.2. 创建一个Java类进行测试 3.3. 创建和运行JUnit测试案例 3.4. 使用@Ignore注释 3.5. 创建测试套件 3.6. 创建参数化测试 3.7. 规则 3.8. 分类 4.在命令行运行JUnit测试 5.结论   1.单元测试的介绍 1.1. 什么是单元测试? 一个单位可以是一个函数,一个类,一个包,或者一个子系统。所以,术语单元测试是指测试这样小单位的代码,以确保它们正常工作。例如,我们可以测试在给定的输入或者某给定条件的情况下,输出是否是我们所希望看到的。这种做法可以帮助开发人员发现代码逻辑背后的故障,提高其代码的质量。另外,单元测试可以用来以确保代码在未来变化中可以正常实现逻辑。 1.2. 测试覆盖 一般情况下,开发社区对关于代码应测试的百分比(测试覆盖)有不同的看法。一些开发者认为代码应该有100%的测试覆盖率,而有些则认为有50%或更低的测试覆盖率也可以。在任何情况下,你应该写测试你的代码中复杂或关键部分。 1.3. 在Java单元测试 在Java中最流行的测试框架是JUnit。本指南主要集中于JUnit,下面的章节有更多测试框架的细节。在Java中另一种流行的测试框架是TestNG。 2. JUnit的介绍 JUnit 是一个开源测试框架,用于编写和运行可重复的自动化测试,让我们可以确保我们的代码按预期工作。 JUnit 广泛应用于行业,可以用作单独的Java程序(在命令行)或在一个IDE如Eclipse内。 JUnit提供: 断言测试预期结果. 测试功能共享通用的测试数据. ...

Read More »