bangviz
asked on
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(ErrorMess age.Inacti ve);
return false;
}
THANKS
Bangviz
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(ErrorMess
return false;
}
THANKS
Bangviz
ASKER
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(ErrorMess age.Inacti ve);
return false;
}
and how do i test this in testValidateMethod() as mentioned above.
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(ErrorMess
return false;
}
and how do i test this in testValidateMethod() as mentioned above.
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?
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?
ASKER
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(ErrorMess age.Inacti ve);
return false;
}
All i need is if i need to test the above how do i do in the testValidateMethod().
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(ErrorMess
return false;
}
All i need is if i need to test the above how do i do in the testValidateMethod().
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
}