Testing in Java

QA is important for production. Testing is important for QA. 

Here I would like to talk about testing in Java application development. It won't refer to project process/life cycle control,  I will only talk something about automatically unit testing.

Test Data Design and Populate

There are always DB interaction in a Java enterprise application, in order to make sure persistence layer and business layer work well, we need to design test data.

Even in some pure java tools application, we need to design some test data to test extrime use cases.

You can use DBUnit or JDBC to populate and clean your test data aournd/after/during test case running. 

Test Case Design

System Test: Business Use Case Test

We can say test case design does really depend on your businesses use cases.  In fact, it is not enough, sometime and always, business use cases testing will always come in stage of  "Integration Test"/"System Test"/"End2End Test".

Unit Test

There are always JUnit test for:

  • A line of java function code
  • All blocks in a method
  • All methods in a java class
  • Interaction between java classes
  • Java classes security and instances in JVM

They are always come during development. 

Test Requirement birth xUnit

As I will list in this chapter, sometime how you want to test your code and which information will be used to assert valid will push you to define or find some useful tools to help your test design.

How to test private method?

Some people use "protected" keywords to replace "private".  I do not think it is a good design. If we want to define some unit test for private methods, it is better to design "white box" test cases for public functions which will invoke private functions.

Unit Test for Servlet

HttpUnit provide ServletRunner to simulate Servlet Container to test servlet without Java application server running.

Unit Test for Spring

We can use SpeingJUnit4ClassRunner to test Spring in standalone JVM.

Unit Test for EJB

There should be a EJB container to test your EJB functions. Normally you have to design your EJB client as test cases and deploy your EJB application in EJB container.

Unit Test for XML

Here I would like to list a third party tool: xmlunit which can be used to check XML content. For example, we can use xmlunit to compare if two XML contents same or not. 

UI Automatic Test

Most of Java Enterprise application provide user interfaces, so normally we can design some automatically test case on user interface:

  • Simulator user manipulate actions in GUI: input data, click some button.
  • Check expected item in UI

Httpunit and vmaggie (to be updated) is used in my current project for GUI automatic testing.  How to select UI automatically test tools: 1) which language for developing; 2) how the tool execution a test and if it satisfies your UI implementation...

Performance Test

Performance test is importance, you can enhance your prouction quality in stress testing, safety testing and you can also get bench mark to define the optimze configuraton parameters.

Normally performance test cases will focus on a business use case.  The test environement: hardware and software should be coherent with deployment ENV.

There are two kinds performance test:

  1. Automatically performance test: using tools Jmeter or LoadRunner to test some use case and it can be intergrated with automatically build.
  2. Some mannually bench mark test: optimize functiona or define benchmark for configurations. It can also use some tools in automaticaly test to prepare test data and launch testing.

猜你喜欢

转载自george-gu.iteye.com/blog/1288331