JUnit and code coverage problem.

63 views
Skip to first unread message

Kim Beom-soo

unread,
Dec 3, 2011, 9:00:00 PM12/3/11
to CPSC410-2011
HI, I'm trying to work on the coverage part, but I got stuck on
testing the Servlets.

In order to test codes that involves httpServletRequest and
httpServletResponse, I can't create these object in Java.

These objects get created when I'm running my web app in my browser.

So my JUnit tests can't cover big portion of my Servlet codes.

By setting up Eclemma and browsing my web app on the other hand, these
codes gets easily covered when I do a login

(because login involves httpServletRequest and httpServletResponse)

So what I want to do is I want to set up Eclemma, browse my web client
and somehow in my Servlet code, I want to automatically run all test
test cases,

stop the server, and then import my coverage.ec file to cover all the
Servlet code that involves httpServletRequest, httpServletResponse and
mu JUnit test

Am I even in the right track?

In other words, what I want to ask you is:

How can I cover both Servlet codes that involves httpServletRequest,
httpServletResponse AND JUnit test cases in Eclemma?

Thanks for reading my confusing quesiton.


Beom-soo Kim

Eric Wohlstadter

unread,
Dec 3, 2011, 10:16:58 PM12/3/11
to cpsc41...@googlegroups.com
Hi,
There is an example of using Java Apache HttpClient to perform a
tests on a Servlet in the AccountServer.zip posted with the Nov. 2nd
lecture on the schedule web page. You could also write tests in
JavaScript using XHR and place all the test Javascript in a blank
test.html page.

Eric

Justin Leung

unread,
Dec 4, 2011, 6:27:10 PM12/4/11
to CPSC410-2011
Hi Eric,

I've tried just using regular URL request to perform tests on a
Servlet and also the code you provided.

While the requests do infact call the Servlet, and the response has
the contents of the JSON Objects in it, Emma shows 0% code coverage
for the Servlet.

We're at the last push for testing where we need to be able to have
emma recognize our tests for Servlets to reach the 80% code coverage.

Any suggestions?
Justin.

Eric Wohlstadter

unread,
Dec 4, 2011, 8:48:37 PM12/4/11
to cpsc41...@googlegroups.com
Hi Justin,
 Emma won't be able to detect when your Servlet runs unless you run the ant script and follow the instructions posted online. If you did follow all the instructions, then I think you are importing the wrong coverage.ec file. Emma creates one coverage.ec file for the Client code making the URL request, and one coverage.ec file for the Servlet code receiving the URL request (because the client and server are different OS processes).

Eric 

Justin Leung

unread,
Dec 4, 2011, 8:54:03 PM12/4/11
to CPSC410-2011
Hi Eric,

Thanks, Is there a way to merge the two .ec files so we can verify
that the combined coverage between the client and server side achieves
80% (As some of the code coverage calls may overlap). Simply taking
the number of instructions covered for the two files wouldn't work as
the coverage may refer to different lines of code within the same
file.

For anyone else looking for the client/server code coverage.. the
client side coverage is located in the directory of your project in
workspace, while the server side coverage is located in the eclipse
installation directory.

Thanks
Justin

Eric Wohlstadter

unread,
Dec 4, 2011, 11:20:35 PM12/4/11
to cpsc41...@googlegroups.com
Yes, you can merge multiple coverage.ec files.

java emma merge -in [data_files] -out [data_file

Or, instead of using command-line, you can select a new Run Configuration for your project in Eclipse, and type "emma" for the main class, and then for Program Arguments, use the format as above.

Eric
Reply all
Reply to author
Forward
0 new messages