Java Reference
In-Depth Information
G
JSFTimer timer = JSFTimer.getTimer();
assertTrue( "Total time to get the response should not be
more than 1600 ms.", timer.getTotalTime() < 1600 );
H
I
PhaseId appPhase = PhaseId.INVOKE_APPLICATION;
assertTrue( "Execution should not be more than 1600 ms.",
timer.getPhaseTime( appPhase ) < 1600 );
}
}
We start by creating a new test case
B
and a test method
C
. We get a valid
JSFSession
D
and get its
JSFClientSession
E
. Next, we click the
PurchaseButton
to initiate a
request to the managed bean
F
. We create a
JSFTimer
to measure the execution time
G
, and we assert that the
totalTime
of the execution is less than 1600 milliseconds
H
.
We can also measure the execution time against some specific
Phase
I
. The
JSFU
nit
API
provides for timing the execution interval upon any of the standard
JSF
phases:
■
RESTORE_VIEW
■
APPLY_REQUEST_VALUES
■
PROCESS_VALIDATIONS
■
UPDATE_MODEL_VALUE
S
■
INVOKE_APPLICATION
■
RENDER_RESPONSE
As we explained in chapters 3 and 4, the main benefit from the test cases is that they
serve as a shield. Once you have your test cases written, you can proceed and refactor
your application mercilessly, and you're assured that as long as the tests pass and the
bar is green, everything is okay. In this sense, performance testing is important. You
can write your tests and assert that the execution of a given method always takes less
than a given time barrier. Now you're free to improve the logic behind that method,
and you'll always be sure that the invocation of the given
URL
will take no more time
than the time barrier allows.
15.8
Summary
Te s t i n g
JSF
applications requires preparation. Black box testing is brittle, limited, and
hard to perform. The white box approach (mocking approach) also has its disadvan-
tages; mock tests are always fine grained, which means that the interaction among the
different components isn't tested well. Also, once written, the tests need to be rewrit-
ten in case of small cosmetic changes to the application.
JSFU
nit, on the other hand, builds on the Cactus project and uses the in-
container testing strategy.
JSFU
nit provides static and performance analysis mecha-
nisms to fully inspect our applications. In this chapter we also showed how to test
RichFaces Ajax components.
Starting with chapter 12, we discussed the challenges encountered when testing the
frontend layer of a sample Java
EE
application. In the next chapter, we talk about one
of the most recent booms in the Java world,
OSG
i, and the modularity that it provides.