Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Junit

Posted on 2014-01-04
2
Medium Priority
?
448 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

773 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