Home » Tag Archives: JUnit (page 5)

Tag Archives: JUnit

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 »

Using junit for something else

junit != unit test Junit is the Java unit testing framework. We use it for unit testing usually, but many times we use it to execute integration tests as well. The major difference is that unit tests test individual units, while integration tests test how the different classes work together. This way integration tests cover longer execution chain. This means ...

Read More »

Testing System.in and System.out with system-rules

Writing unit tests is an integral part of software development. One problem you have to solve when your class under test interacts with the operating system, is to simulate its behaviours. This can be done by using mocks instead of the real objects provided by the Java Runtime Environment (JRE). Libraries that support mocking for Java are for example mockito ...

Read More »

Separating Integration Tests from Unit Tests Using Maven Failsafe & JUnit @Category

Why Unit Tests Should Run Separately From Integration Tests TDD at the Unit Testing level is fairly straight-forward, since classes in unit testing either do not have complex dependencies, or you mock-out the dependencies with a mocking framework (ex. Mockito). However, TDD quickly becomes difficult when we get to Integration Testing. Integration Testing is basically testing a component with some or all ...

Read More »

Given When Then in Java

tl;dr you can use labels to clarify a given-when-then style of testing. What is given-when-then? given-when-then is a commonly used style of specifying system behaviour in which your tests are split into three sections. Given is the section that lays out the pre-conditions for the test, ie whatever state you’re assuming the world to be in before you start. The ...

Read More »

Testing with files and directories in JUnit with @Rule

Testing with Files and directories in JUnit is easy thanks to TemporaryFolder @Rule. In JUnit rules (@Rule) can be used as an alternative or an addition to fixture setup and cleanup methods (org.junit.Before, org.junit.After, org.junit.BeforeClass, and org.junit.AfterClass), but they are more powerful, and can be more easily shared between projects and classes. The code to be tested   public void ...

Read More »