junit testcase

Hi,

Can anyone assist with example with JUNIT testing.

How can i test this boolean method which checks for
client is active or inactive using junit TestCase.

This is the code which i need to test the method validate
checks if the passing client object is active or not else
throws the error message

public boolean validate(Object obj, String message) {


      if (obj.getStatus == "active"){
          return true;
      }
      message.setValue(ErrorMessage.Inactive);
          return false;

    }
   
THANKS
Bangviz



LVL 1
bangvizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lhankinsCommented:
Define a new class which extends Testcase and which overrides the setup() and teardown() methods.  

In looking at the signature of your validate method, you'll have to change it slightly (you can't pass Object here and expect to call .getStatus() on it - there is no getStatus() method on Object).   You'll need to pass your own class here (in the example below, this is the "Foo" class I've added).   Also - you can't set the message parameter the way you're trying to do, I've moved this to the "ValidationResult" class which is a wrapper for a boolean + a message.   See example below:

---------------------------


import junit.framework.TestCase;

public class SampleTest extends TestCase
{
   public SampleTest()
   {
   }

   public SampleTest(String s)
   {
      super(s);
   }

   protected void setUp() throws Exception
   {
      // do any common initialization stuff here...
   }

   protected void tearDown() throws Exception
   {
      // do any common cleanup stuff here...
   }


   public void testValidateMethod() throws Exception
   {
      //--- test a good object...
      Foo someObject = new Foo("active");

      ValidationResult result = validate(someObject);

      assertTrue(result.isValid());
      assertEquals("valid", result.getMessage());

      //--- test an object which should fail validation...
      someObject = new Foo("inactive");

      result = validate(someObject);

      assertFalse(result.isValid());
      assertEquals("invalid", result.getMessage());
   }

   
   
   /**
    * new signature for validate method
    */
   
   public ValidationResult validate(Foo obj)
   {
      if (obj.getStatus() == "active")
      {
         return new ValidationResult(true, "valid");
      }

      return new ValidationResult(false, "invalid");
   }


   /**
    * new class which wraps validation result...
    */
   class ValidationResult
   {
      private String message;
      private boolean isValid;

      public ValidationResult(boolean aValid, String aMessage)
      {
         isValid = aValid;
         message = aMessage;
      }

      public boolean isValid()
      {
         return isValid;
      }

      public String getMessage()
      {
         return message;
      }
   }

   
   /**
    * sample object to validate (replace with your object that implements a getStatus() method).
    */
   
   class Foo
   {
      private String status;


      public Foo(String aStatus)
      {
         status = aStatus;
      }

      public String getStatus()
      {
         return status;
      }

      public void setStatus(String aStatus)
      {
         status = aStatus;
      }
   }


}
0
bangvizAuthor Commented:
Your right I'm actually doing this validation using a baseproduct which has its the required api and I use those methods which are part of it.

My question I need to pass even the string message as shown here in this boolean method(which is part of the  base product)

public boolean validate(Object obj, String message) {

     if (obj.getStatus == "active"){
         return true;
     }
     message.setValue(ErrorMessage.Inactive);
         return false;

    }

and how do i test this in  testValidateMethod() as mentioned above.
0
lhankinsCommented:
I'm not sure I follow you here...

In the validate method you have listed Object and String as parameter types, I assume this is the default java.lang.Object and java.lang.String.   That method will not compile for a couple of reasons :

1. There is no getStatus() member on java.lang.Object.
2. There is no setValue() method on java.lang.String

Am I missing something?
0
bangvizAuthor Commented:
These methods are part of the base product which i'm using...
and i have given a pseudocode for validation and these are not part of default java.lang.object nor java.lang.string.

Baically this object is a client object with all there details and String which holds validation message

public boolean validate(Object obj, String message) {

     if (obj.getStatus == "active"){
         return true;
     }
     message.setValue(ErrorMessage.Inactive);
         return false;

    }


All i need is if i need to test the above how do i do in the testValidateMethod().
0
lhankinsCommented:
So does the following make sense then...?    Add a method called testValidateMethod() which creates  some sample objects, calls the validate method, and then uses JUnit assertXXX methods to assert that the results are as expected.    For Example (obviously you'll have to adjust this to fit your third party tool API, but this demonstrates the concept) :

   public void testValidateMethod() throws Exception
   {
      //--- test a good object...
      Foo someObject = new Foo("active");

      ValidationResult result = validate(someObject);

      //--- JUnit provides these assertXXX methods...
      assertTrue(result.isValid());
      assertEquals("valid", result.getMessage());

      //--- test an object which should fail validation...
      someObject = new Foo("inactive");

      result = validate(someObject);

      assertFalse(result.isValid());
      assertEquals("invalid", result.getMessage());
   }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.