[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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



0
bangviz
Asked:
bangviz
  • 3
  • 2
1 Solution
 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now