Unit Test and Integration Test using Maven+JUnit4

Unit Testing

Unit testing is the practice of testing small pieces of code, typically individual functions, alone and isolated. Unit testing is the backbone. You can use unit tests to help design your code and keep it as a safety net when doing changes. Unit tests are also great for preventing regressions

Integration Testing

In integration testing, the idea is to test how parts of the system work together – the integration of the parts. Integration tests are similar to unit tests, but there’s one big difference: while unit tests are isolated from other components, integration tests are not.

Functional Testing

Functional testing is also sometimes called E2E testing, or browser testing. They all refer to the same thing.
Functional testing is defined as the testing of the complete functionality of some application. In practice with web apps, this means using some tool to automate a browser, which is then used to click around on the pages to test the application.

Using JUnit Categories and Maven to Split Unit Testing and Integration Testing

Define A Marker Interface

The first step in grouping a test using categories is to create a marker interface.
This interface will be used to mark all of the tests that you want to be run as integration tests.

public interface IntegrationTest {}

Mark your test classes

Add the category annotation to the top of your test class. It takes the name of your new interface.

import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class ExampleIntegrationTest{
  @Test
  public void longRunningServiceTest() throws Exception {
  }
}

Configure Maven Unit Tests

The beauty of this solution is that nothing really changes for the unit test side of things.
We simply add some configuration to the maven surefire plugin to make it to ignore any integration tests.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.11</version>
  <dependencies>
   <dependency>
     <groupId>org.apache.maven.surefire</groupId>
     <artifactId>surefire-junit47</artifactId>
     <version>2.12</version>
   </dependency>
  </dependencies>
  <configuration>
    <includes>
      <include>**/*.class</include>
    </includes>
    <excludedGroups>com.test.annotation.type.IntegrationTest</excludedGroups>
  </configuration>
</plugin>

When you do a mvn clean test only your unmarked unit tests will run.

Configure Maven Integration Tests

Again the configuration for this is very simple.
To run only the integration tests, use this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.11</version>
  <dependencies>
   <dependency>
     <groupId>org.apache.maven.surefire</groupId>
     <artifactId>surefire-junit47</artifactId>
     <version>2.12</version>
   </dependency>
  </dependencies>
  <configuration>
    <groups>com.test.annotation.type.IntegrationTest</groups>
  </configuration>
</plugin>

If you wrap this in a profile with id IT, you can run only the fast tests using mvn clean install. To run just the integration/slow tests, use mvn clean install -P IT.

Reference

  1. https://stackoverflow.com/questions/2606572/junit-splitting-integration-test-and-unit-tests/10381662#10381662
  2. https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional-testing/
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 解决真机和模拟器调试报错,分别打开文件路径/Applications/Xcode.app/Contents/Dev...
    Coopin阅读 1,180评论 0 0
  • 今天是鬼节,但是我并不知道。 微信上,梅梅问我,你知道今天是什么节日吗?我表示不知道,我翻过日历,看了一下日期,并...
    东田南阅读 220评论 0 1
  • 一根燃尽的残烟勾勒出你熟悉的姿势,不变的是你那焦黄的食指与中指间夹着愁绪,灼灼点燃的父爱如烛火拉开了黑夜! 小时候...
    猜度下一页阅读 409评论 3 5
  • 2018年3月22日 星期四 阴 今天下午在家没事,读《父母规》3天了,一直没敢录音,在家...
    张宗欣阅读 262评论 0 0
  • 感恩环卫阿姨,每次见面都是热情的打招呼。感恩她如此的辛苦,工作还是这么认真努力。以前曾和阿姨聊过,她就是这附近的居...
    武丹yoyo阅读 142评论 0 2