• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New to JUnit. How to Write a Unit Testing Code?

 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question may sound like a student asking people to help with my homework. However, I am not a student. And I start writing Junit to test each method.

I use the Eclipse. I have add the junit-4.8.2.jar in my project build path. What does JUnit test code look like for the method shown below? If anybody would spare time to guide me.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Natalie,
It doesn't sound like homework at all.

The first few things I think of:
1) One doesn't usually unit test a main method. No reason you can't; just that the logic doesn't generally reside there. Of course, it is just a main method so you can call it from a unit test.
2) What is ApplicationService? Do you want to test it or mock it out?
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne, thanks for your response. I appreciate that you are willing to spare your time to help.

I am sorry for not explaining what I did in my code. I am working on a National Cancer Institute (NCI) project. Developers cannot direct access NCI data system.

The system provides developers with APIs to access domain objects and all service methods using any of the provided interfaces. And the ApplicationService is one of the interfaces.

The basic order of operations to access and use the system is as follows:

1.Ensure that the client application has knowledge of the objects in the domain space.
2.Formulate the query criteria using the domain objects.
3.Establish a connection to the server.
4.Submit the query objects and specify the desired class of objects to be returned.
5.Use and manipulate the result set as desired.

This code creates an instance of a class that implements the ApplicationService interface. This interface defines the service methods used to access data objects. A criterion object, i.e., the taxonList, is then created that defines the attribute values for which to search. The search method of the ApplicationService implementation is called with parameters that indicate the type of objects, i.e., Gene.class, to return and the criteria that returned objects must meet, defined by the taxonList object.

The code shown in my previous posting worked successfully to return the search result.

I have no clue in regard to write a unit test for this kind of operation.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry if I'm being dense. I didn't see the answer to whether you want to include the call to "List resultList = appService.search(Gene.class, taxonList); " in your test.

If you do, it is an integration test. If not, it is a unit test and you need a framework like jMock or Mockito to dummy out the call.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic