Solved

Junit

Posted on 2014-01-04
2
441 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 20

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
Comment Utility
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
Comment Utility
Thank you for good comments, it is working fine
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now