Thursday, 2 July 2015

JQuery: Failed to load resource: net::ERR_FILE_NOT_FOUND

JQuery: Failed to load resource:  net::ERR_FILE_NOT_FOUND

If you are getting the above error that means the html page is unable to load the Jquey library file which you are using.

1.       If you are trying to load JQuery library from CDN, then make sure to mention the http:// or https:// in the script tag.
For ex:- if you mention the src file as below, then by default it will look for file://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js which it is unable to load so giving the error File not found.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

The correct syntax is to mention the complete url starting with http:// as below.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

2.       If you are trying to load the JQuery library file from local repository, make sure to give the correct path to the library file. If you have the below directory structure, then reference the .js file as <script src="jquery/jquery-1.11.1.min.js"></script>


dir1
|------sample.html
|------jquery
             |---------jquery-1.11.1.min.js


Failed to load resource: net::ERR_FILE_NOT_FOUND    file:///D:/Jquery/text/javascript

If you are getting the above error, that means html is unable to load “text/javascript” that is mentioned in the script tag. It may be because of the incorrect syntax of the <script> tag.

In my case I have mentioned “src” attribute instead of “type” in the script tag like below.
<script src="text/javascript"></script>
Correct syntax is to use type attribute
<script type="text/javascript"></script>

After changing this, the error is gone. So make sure to check on the syntax.

Wednesday, 24 June 2015

JUnit TestSuite: Running multiple test cases together.

Multiple Test cases can be run together in JUnit with Test Suite. Let’s say we have CalcTest.java and CalcTest1.java which need to be run together.

1.       Let’s create the class Calc.Java whose methods need to be tested. It has two methods add and subtract which will do the simple addition and subtraction of 2 numbers.

package com.main.java; 

public class Calc {
  
   public int add(int number1, int number2)
   {
          return number1+number2;
   }
  
   public int subtract(int number1, int number2)
   {
          return number1-number2;
   }

}


2.       Let’s Create CalcTest.java to test the add() method of Calc.java

package com.main.java; 

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class CalcTest {

       int checkValue;
       int value1;
       int value2;

       @Before
       public void setUp()
       {
       value1 = 5;
       value2 = 8;
       checkValue=13;
       }

       @Test
       public void testAdd() {
       assertTrue(checkValue==new Calc().add(value1, value2));
       }

}

3.       Let’s create another Test class CalcTest1.java to test the subtract() method of the Calc.java

package com.main.java;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class CalcTest1 {

       int value1;
       int value2;

       @Before
       public void setUp()
       {
       value1 = 8;
       value2 = 5;      
       }

       @Test
       public void testSubtract() {
              assertTrue(3==new Calc().subtract(value1, value2));
       }

}

4.       Now create a JUnit Test Suite in eclipse, by clicking on File-> New -> Other -> JUnit Test Suite. Select the test classes to be included in the Test Suite as shown below in the screen shot.

Note:  Make sure to select the New JUnit 4 Suite on top otherwise the test classes won’t be displayed for selecting.


5.       As you can see, it has created the TestSuit with the name AllTests  with the 2 annotations. The annotations  @SuiteClasses and @RunWith are required for running the class as TestSuite.
In the @SuiteClasses annotation, both the test classes selected above are populated.

package com.main.java;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ CalcTest.class, CalcTest1.class })
public class AllTests {

}

6.       In the TestRunner.Java class file, give the TestSuite name i.e AllTests in the JUnitCore RunClasses() method. It will run both the test case classes mentioned above.

package com.main.java;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {

       public static void main(String args[])
       {
              Result result = JUnitCore.runClasses(AllTests.class);
             
                     for(Failure failure : result.getFailures())
                     {
                           System.out.println(failure.toString());
                     }
             
              System.out.println(result.wasSuccessful());
       }

}

Tuesday, 23 June 2015

Eclipse: Sample JUnit Test case Application

Sample JUnit Appliction in Eclipse

1.     Create a new project in Eclipse, let's say JUnitSample and setup the environment as mentioned in the previous post.
2.     Create a new Java class say Calc.Java under src.  This class has methods to perform the
      Arithmetic operations.
       Let’s create a method say add() which adds 2 numbers. It simply returns the sum of
       two integers.

package com.main.java;

public class Calc {

public int add(int number1, int number2)
{
return number1+number2;
}

}

3.      Now create a Junit Test case “CalcTest.Java” for the class “Calc”. select the class Calc and the method Add() while creating the JUnit test case in eclipse.

4.      Now you can see, there is a method added testAdd() with the annotation @Test.  The annotation signifies that the method will be run when we run the CalcTest test cases.

5.      Now let's add SetUp() method to predefine the values as shown in the example. Prefix it with @Before so that the method will be run before each test method runs.

6.      As you see in the below example, in the testAdd() method we are using assertTrue() method to check the value returned by add() method. It returns error, if the values doesn't match.

package com.main.java;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class CalcTest {

int checkValue;
int value1;
int value2;

@Before
public void setUp()
{
value1 = 5;
value2 = 8;
checkValue=13;
}

@Test
public void testAdd() {
assertTrue(checkValue==new Calc().add(value1, value2));
}

}

7.    Now we will see how to run the test case. Let's create TestRunner.Java.


As you see in the below example, in the main method, we are using JUnitCore.runClassses() method to run the CalcTest test cases. It will run all the methods inside the CalcTest.Java which are annotated with @Test.

The result will be stored in Result and using the methods getFailures() and wasSuccessful() to get the failures and status respectively. If executed with the above values the example should print "true".

package com.main.java;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {

public static void main(String args[])
{
Result result = JUnitCore.runClasses(CalcTest.class);
for(Failure failure : result.getFailures())
{
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());
}
}

Note:  You can run JUnit Testcases from the eclipse, simply by right clicking on the project -> Select Run AS -> select  JUnit Test. It will run all the test cases created.

Eclipse: Env Setup and Creating and Running a JUnit Test case.

Below are the steps to create and Run a JUnit Test case form the Eclipse

1.    Download the Junit jar from the location http://junit.org/ and also download the
     dependency jar hamcrest-core.jar.

2.    Create a new project in Eclipse. Right click on Project -> Properties -> Java Build Path -> Go to Libraries tab -> click on Add external Jars. Select the downloaded jars and add them.

3.     For creating a Junit test case, Right click on Project -> New -> Other -> Java -> Junit -> Select Junit Test Case.

a.     In the Next Window, Enter the name for the Junit Test Case and also select the Java class for which the test class is being created. Click on Next

b.     In the Next Window, Select the available methods for which the test method should be added and click on Finish.

4.    For running the test case, Right click on Project -> Run As -> JUnit Test. It should give the results accordingly.


If you are using Maven, Add the below dependency to pom.xml for JUnit. It will download the  JUnit and the hamcrest-core jars.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

Maven Plugin Error while creating the project in Eclipse


While creating the Maven project in Eclipse, If we get the below error it is due to the missing of the build life cycle mapping for m2e plugin.

- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile
 (execution: default-testCompile, phase: test-compile)
- CoreException: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-compiler-plugin:2.5.1 or one of its
 dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1:

Solution:

1.     Eclipse is providing the quick fix for ignoring the goal compile. Right click on the error and choose quick fix and select “Permanently mark goal compile in pom.xml as ignored”.  It will modify the pom.xml to ignore the maven-compiler-plugin for the goals.  Do it for compile and test Compile goals.
2.     Another way is to configure the same at the workspace level, go to Windows-> Preferences -> Maven -> Lifecycle Mappings. You can edit and reload the changed xml file.

Please see the more info on this common problem at the link:  https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html

Please note I have used Eclipse Luna Version and Maven 3.3.3 versions. 

Sunday, 21 June 2015

Windows: "ftp" is not recognized as an internal or external command

In Windows OS, If you are getting the above error upon entering ftp in the command prompt follow the below steps.

1. Check whether "ftp.exe" is present in your C:/Windows/System32 path.

2. If present, set the PATH environment variable to point to this path.  Click on My Computer-> Properties -> Advanced System Settings -> Click on Environment Variables -> Select the PATH under System Variables and append the path to the "ftp.exe" at the end.

Friday, 19 June 2015

Junit Error: assertEquals() method undefined for the type

There are 2 ways to fix this error depending on the junit version you are using.

1. If you are using Junit 4.X,  you need to have the following import statement 
    import static org.unit.Assert.* 

   This Assert class contains the set of assertion methods useful for writing tests. 
   For ex it has assertArrayEquals(), assertEquals(), assertFalse(), assertTrue() etc. 

   All these static methods can be referenced with out prefix in the code.

   Please refer to the below link for more info on static class.


2. If you are using Junit 3.X , you need to import junit.framework.TestCase and your
    test class should extend the TestCase. By extending the junit.framework.TestCase,

    It inherits junit.framework.Assert default and your test class will have the access
    to the Assertion methods.