Solved

Integration Testing in eclipse

Posted on 2011-03-12
6
409 Views
Last Modified: 2012-05-11
Is there any good plugins in eclipse that does integration testing? To test method calls between specified classes.
0
Comment
Question by:Atouray
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 35117954
There's built in support for JUnit (http://www.junit.org/) and this article explains using it within Eclipse (http://www.junit.org/node/49).

There's nothing about JUnit that limits you to writing unit tests.  You can easily write integration tests - where your test classes just calls to methods in other classes.

Doug
0
 

Author Comment

by:Atouray
ID: 35118729
I have two classes, and one of them is calling a method from the other. I want to do an integration testing for this.
Class FirstClass{

 public void openDocument(URL url) throws Exception {
        // Originally, this method determined external application, with which the document
        // should be opened. Which application should open which document type was
        // configured in Vmap properties file. As a result, Vmap tried to solve the
        // problem (of determining application for a file type), which should better be
        // solved somewhere else. Indeed, on Windows, this problem is perfectly solved by
        // Explorer. On KDE, this problem is solved by Konqueror default browser. In
        // general, most WWW browsers have to solve this problem.

        // As a result, the only thing we do here, is to open URL in WWW browser.

        String osName = System.getProperty("os.name");
        if (osName.substring(0,3).equals("Win")) {
            String propertyString = new String("default_browser_command_windows");
			if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
				propertyString += "_9x";
			} else {
				propertyString += "_nt";
			}

            String browser_command=new String();
            String command=new String();
            // Here we introduce " around the parameter of explorer
            // command. This is not because of possible spaces in this
            // parameter - it is because of "=" character, which causes
            // problems. My understanding of MSDOS is not so good, but at
            // least I can say, that "=" is used in general for the purpose
            // of variable assignment.
            //String[] call = { browser_command, "\""+url.toString()+"\"" };
            try  {
                // This is working fine on Windows 2000 and NT as well
                // Below is a piece of code showing how to run executables directly
                // without asking. However, we don't want to do that. Explorer will run
                // executable, but ask before it actually runs it.
                //
                // Imagine you download a package of maps containing also nasty
                // executable. Let's say there is a map "index.mm". This map contains a
                // link to that nasty executable, but the name of the link appearing to the
                // user does not indicate at all that clicking the link leads to execution
                // of a programm.  This executable is located on your local computer, so
                // asking before executing remote executable does not solve the
                // problem. You click the link and there you are running evil executable.

                // build string for default browser:
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty(propertyString));
                browser_command = formatter.format(messageArguments);
        
                if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+url.toString(); 
                } else {
                    command = browser_command; 
                }
                Runtime.getRuntime().exec(command); 
            }
            catch(IOException x) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+command+"\".\n\nYou may look at the user or default property called '"+propertyString+"'.");
                System.err.println("Caught: " + x); 
            }
        } else if (osName.startsWith("Mac OS")) {
             String urlString = url.toString();
             String browser_command=new String();
            try {
                // build string for default browser:
                if (url.getProtocol().equals("file")) {
                    urlString=urlString.replace('\\','/').replaceAll(" ","%20"); 
                }

                Object[] messageArguments = { urlString, urlString };
                // ask for property about browser: fc, 26.11.2003.
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_mac"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); 
            } catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_mac'.");
                System.err.println("Caught: " + ex2); 
            }
        } else {
            // There is no '"' character around url.toString (compare to Windows code
            // above). Putting '"' around does not work on Linux - instead, the '"'
            // becomes part of URL, which is malformed, as a result.	 

//             String urlString = url.toString();
//             if (url.getProtocol().equals("file")) {
//                 urlString = urlString.replace('\\','/').replaceAll(" ","%20"); }
//             // ^ This is more of a heuristic than a "logical" code

            String browser_command=new String();
            try {
                // build string for default browser:
                String correctedUrl = new String(url.toExternalForm());
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { correctedUrl, url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_other_os"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); }
            catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_other_os'.");
                System.err.println("Caught: " + ex2); 
            }
        }
    }

}



Class Tools{

 public static String urlGetFile(URL url) {
       String osNameStart = System.getProperty("os.name").substring(0,3);
       String fileSeparator = System.getProperty("file.separator");
       if (osNameStart.equals("Win") && url.getProtocol().equals("file")) {          
          String fileName = url.toString().replaceFirst("^file:","").replace('/','\\');
          return (fileName.indexOf(':') >= 0) ?
             fileName.replaceFirst("^\\\\*","") :
             fileName; } // Network path
       else {
          return url.getFile(); }}


}

Open in new window

0
 

Author Comment

by:Atouray
ID: 35118744
I have to test the coupling between these classes.
  if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {


The method Tools.urlGetFile(url) is in the Tools method. How do i go about with the integration testing. This is what I have done so far but i believe it is more of a unit test than an integration test. Can someone guide me through the integration testing?
@Test
      public void T2() throws MalformedURLException {
            try{
                  URL url=new URL("http://www.yahoo.com/edinburgh/lothian/warrender");
                  
                  m_vmap.openDocument(url);
                  
                  String urlInTools = m_cCountField.urlGetFile(url);
                  System.out.println("URLGetFile: "+urlInTools);
                  // formatter.format(url);
                  //assertEquals("/edinburgh/lothian/warrender",urlInTools);
            }
            catch(Exception e) {
                  System.out.println("Error Found: "+e);
                  assertTrue(true);
            }
      }

Thank you
Class FirstClass{

 public void openDocument(URL url) throws Exception {
        // Originally, this method determined external application, with which the document
        // should be opened. Which application should open which document type was
        // configured in Vmap properties file. As a result, Vmap tried to solve the
        // problem (of determining application for a file type), which should better be
        // solved somewhere else. Indeed, on Windows, this problem is perfectly solved by
        // Explorer. On KDE, this problem is solved by Konqueror default browser. In
        // general, most WWW browsers have to solve this problem.

        // As a result, the only thing we do here, is to open URL in WWW browser.

        String osName = System.getProperty("os.name");
        if (osName.substring(0,3).equals("Win")) {
            String propertyString = new String("default_browser_command_windows");
			if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
				propertyString += "_9x";
			} else {
				propertyString += "_nt";
			}

            String browser_command=new String();
            String command=new String();
            // Here we introduce " around the parameter of explorer
            // command. This is not because of possible spaces in this
            // parameter - it is because of "=" character, which causes
            // problems. My understanding of MSDOS is not so good, but at
            // least I can say, that "=" is used in general for the purpose
            // of variable assignment.
            //String[] call = { browser_command, "\""+url.toString()+"\"" };
            try  {
                // This is working fine on Windows 2000 and NT as well
                // Below is a piece of code showing how to run executables directly
                // without asking. However, we don't want to do that. Explorer will run
                // executable, but ask before it actually runs it.
                //
                // Imagine you download a package of maps containing also nasty
                // executable. Let's say there is a map "index.mm". This map contains a
                // link to that nasty executable, but the name of the link appearing to the
                // user does not indicate at all that clicking the link leads to execution
                // of a programm.  This executable is located on your local computer, so
                // asking before executing remote executable does not solve the
                // problem. You click the link and there you are running evil executable.

                // build string for default browser:
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty(propertyString));
                browser_command = formatter.format(messageArguments);
        
                if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+url.toString(); 
                } else {
                    command = browser_command; 
                }
                Runtime.getRuntime().exec(command); 
            }
            catch(IOException x) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+command+"\".\n\nYou may look at the user or default property called '"+propertyString+"'.");
                System.err.println("Caught: " + x); 
            }
        } else if (osName.startsWith("Mac OS")) {
             String urlString = url.toString();
             String browser_command=new String();
            try {
                // build string for default browser:
                if (url.getProtocol().equals("file")) {
                    urlString=urlString.replace('\\','/').replaceAll(" ","%20"); 
                }

                Object[] messageArguments = { urlString, urlString };
                // ask for property about browser: fc, 26.11.2003.
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_mac"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); 
            } catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_mac'.");
                System.err.println("Caught: " + ex2); 
            }
        } else {
            // There is no '"' character around url.toString (compare to Windows code
            // above). Putting '"' around does not work on Linux - instead, the '"'
            // becomes part of URL, which is malformed, as a result.	 

//             String urlString = url.toString();
//             if (url.getProtocol().equals("file")) {
//                 urlString = urlString.replace('\\','/').replaceAll(" ","%20"); }
//             // ^ This is more of a heuristic than a "logical" code

            String browser_command=new String();
            try {
                // build string for default browser:
                String correctedUrl = new String(url.toExternalForm());
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { correctedUrl, url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_other_os"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); }
            catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_other_os'.");
                System.err.println("Caught: " + ex2); 
            }
        }
    }

}



Class Tools{

 public static String urlGetFile(URL url) {
       String osNameStart = System.getProperty("os.name").substring(0,3);
       String fileSeparator = System.getProperty("file.separator");
       if (osNameStart.equals("Win") && url.getProtocol().equals("file")) {          
          String fileName = url.toString().replaceFirst("^file:","").replace('/','\\');
          return (fileName.indexOf(':') >= 0) ?
             fileName.replaceFirst("^\\\\*","") :
             fileName; } // Network path
       else {
          return url.getFile(); }}


}

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 35119971
I'm not very clear on how you see the two methods as being related.

An integration test usually takes the form:
 
public void myTest() {
     IntermediateData myData = callFirstMethod() ;
     assertTrue(myData == expectedIntermediateValue) ; 
    
     FinalResult final = callSecondMethod(myData) ;
    assertTrue(final == expectedFinalValue) ; 
 }

Open in new window


At the moment your test is more taking the form:
 
public void myTest() {
    callMethod1() ;

    // This method should depend in some way on the result from method1
    callMethod2() ;
}

Open in new window



I hope that helps,

Doug
0
 

Author Comment

by:Atouray
ID: 35124266
Thank you. I did something similar to what you have done, is this integration as well?
public void testCouplingPath() throws MalformedURLException{
		
		//Testing for the operating System
		String ExpectedSystemOS = "Win";
		String osName = System.getProperty("os.name");
		String actualSystemOS = osName.substring(0,3);
		assertEquals("System OS type",ExpectedSystemOS, actualSystemOS);
		
		//Testing for the url protocol
		URL url = new URL("file://www.gambia.west.africa.gm");
		String urlProtocol = url.getProtocol();
		String expectedProtocol = "file";
		assertEquals("URL Protocol",expectedProtocol,urlProtocol);
		
		//testing for the Tools.urlGetFile(url)
		String urlGetFileReturns = Tools.urlGetFile(url);
		String expectedURLReturn = "\\\\www.gambia.west.africa.gm";
		assertEquals("urlGetFile return answer",expectedURLReturn,urlGetFileReturns);
		
	
	}

Open in new window

0
 
LVL 26

Expert Comment

by:dpearson
ID: 35124780
That looks like a good test but it's still really just a unit test for urlGetFile() and some parts of the openDocument() method.

I think to make it a full integration test you just need to add a call to:
FirstClass.openDocument(url);
probably at the end of the test.

Since openDocument() doesn't actually return anything, it's hard to test it's behavior - except that a call to it doesn't throw an exception.

Doug
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
simple java question 3 55
reverse digits of a number using for loop 5 40
eclipse buid path vs tomcat lib path 10 22
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This video teaches viewers about errors in exception handling.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

776 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