Labels

Sunday, 19 January 2014

JUnit

1. Declare this is a Test method:
@Test 
public void method()

2. This method is executed before each Test:
Note: used to prepare the test environment (i.e. Read input files..)
@Before
public void method()

3. This method is executed after each Test:
Note: To cleanup environment (i.e. Delete temporary file, data..)
@After
public void method()

4.This method is executed ONLY once before start of All tests.
Note: To perform time intensive activities (i.e. DB connections..) 
@BeforeClass
public static void method()

5. This method is executed ONLY once after all tests.
Note: i.e. To disconnect DB
@AfterClass
public static void method()

6. To ignore one test method:
Note: Do not execute this method
@Ignore
some_method()

7, Fail the test if the method takes longer than n milliseconds:
Note: Fails the test if it takes > 100 milliseconds
@Test(timeout=100)
some_method()

8. Fail the test if method NOT throwing the named exception;
Note; Test has to throw out the specified exception.
@Test(expected=Some_Exception.class)
some_method()

9. Hamcrest
assertThat( actual, is(expected value));
































Hamcrest comes with a library of useful matchers. Here are some of the most important ones.
  • Core
    • anything - always matches, useful if you don't care what the object under test is
    • describedAs - decorator to adding custom failure description
    • is - decorator to improve readability - see "Sugar", below
  • Logical
    • allOf - matches if all matchers match, short circuits (like Java &&)
    • anyOf - matches if any matchers match, short circuits (like Java ||)
    • not - matches if the wrapped matcher doesn't match and vice versa
  • Object
    • equalTo - test object equality using Object.equals
    • hasToString - test Object.toString
    • instanceOfisCompatibleType - test type
    • notNullValuenullValue - test for null
    • sameInstance - test object identity
  • Beans
    • hasProperty - test JavaBeans properties
  • Collections
    • array - test an array's elements against an array of matchers
    • hasEntryhasKeyhasValue - test a map contains an entry, key or value
    • hasItemhasItems - test a collection contains elements
    • hasItemInArray - test an array contains an element
  • Number
    • closeTo - test floating point values are close to a given value
    • greaterThangreaterThanOrEqualTolessThanlessThanOrEqualTo - test ordering
  • Text
    • equalToIgnoringCase - test string equality ignoring case
    • equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
    • containsStringendsWithstartsWith - test string matching

Maven - Troubleshooting

1. Re-download MAVEN repository: 
Only download repository dependency, not test the code.
  • mvn clean compile -DskipTests=true
2. Proxy - (In case proxy used in Workplace): 
   Note: Set Proxy first; then download repository
    2.1 Locate "Auto Proxy Script"
  • Control Panel/ Internet Options/ Connections Tab/ LAN Settings 
    2.2 Download "Proxy Data File"
    2.3 Select a proxy setting as below:
  • var some_id = "PROXY some_host.com:some_port";
    2.4 Now set proxy in settings.xml config file:
         Note: some one save it under .../.m2/ folder 
  • Open settings.xml from %M2_HOME%\conf directory
  • Edit settings.xml as below:
      <proxy>
              <id> some_id </id>
              <active> true </active>
              <protocol> http </protocol>
              <host> some_host </host>
              <port> some_port </port>
      </proxy>

3. Force Maven to Re-download A particular Repository
    When Maven encounters a problem and unable to download A repository --> We need to use this command to re-download that Repo after problem has been solved.

  • mvn dependency:purge-local-repository
4. Force Maven to Update dependencies
    Note: Used for update the jar files
  • mvn clean compile -U -DskipTests=true