Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Junit

Posted on 2014-01-04
2
443 Views
Last Modified: 2014-01-04
public Split process(Split split) throws Exception {

		if (split.getFrom().equals(split.geTo())) {
			StringBuffer stringBuffer = new StringBuffer();
	stringBuffer.append("The 'FROM and TO Member ID' should not be matched: (");
			stringBuffer.append(split.getMidFrom());
			stringBuffer.append(",");
			stringBuffer.append(split.getMidTo());
			stringBuffer.append(")");
			throw new ValidationException(stringBuffer.toString());
		}
		
		if (null == split.getFrom() || "".equals(split.getFrom().trim())) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("'FROM  ID' is missing from the record: (");
			stringBuffer.append(split.getMidFrom());
			stringBuffer.append(",");
			stringBuffer.append(split.getMidTo());
			stringBuffer.append(")");
			throw new ValidationException(stringBuffer.toString());
		}
		
		if (null == split.getTo() || "".equals(split.getTo().trim())) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("'TO  ID' is missing from the record: (");
			stringBuffer.append(split.getFrom());
			stringBuffer.append(",");
			stringBuffer.append(split.getTo());
			stringBuffer.append(")");
			throw new ValidationException(stringBuffer.toString());
		}
		
		return split;
	}

Open in new window


is this correct way to write  junit

public class SplitProcessorTest {
	private Split split;
	

    @Before
    public void beforeTest() {
    	split = new Split ();
    	
    }

    @Test
    public void SplitdNotsameTest() throws Exception {
    	
    	split.setFrom ("Test1");
    	split.setTo("Test1");
    	
    	try{
    		
assertNotSame("("The 'FROM and TO  ID' should not be matched: ", split.getFrom(),split.getTo());
    		  }catch(AssertionError exception){ }
    		  }
    	
   
    
    
    @Test
    public void SplitfromNullTest() throws Exception {
    	
    	split.setFrom(" ");
    	split.setTo("Test1");
    	
    	try{
    		
    		assertNull("From member id is missing from the record",null == split.getFrom() || "".equals(split.getFrom().trim()));    		
    	
    		  }catch(AssertionError exception){ }
    		  }
    	
   
    
    @Test
    public void SplitTonullTest() throws Exception {
    	
    	
    	split.setMidTo(" ");
    	
    	try{
    		
    		assertNull("To member id is missing from the record",null == split.getTo() || "".equals(split.getTo().trim()));    		
    	
    		  }catch(AssertionError exception){ }
    		  }
    	
    
    
    
    	
}	
    

Open in new window


thanks
0
Comment
Question by:roy_sanu
2 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
ID: 39756380
You have just verified based on the values set in Split.

You should also call process() method in your test case and check the expected exceptions if any.


Refer : http://www.mkyong.com/unittest/junit-4-tutorial-2-expected-exception-test/
0
 

Author Comment

by:roy_sanu
ID: 39757053
Thank you for good comments, it is working fine
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
HashTable highest marks enumeration alternative 9 43
jboss wildfly 10.1 10 228
ejb stateless example 2 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question