Translate

25 April 2017

Testing NodeJS


Testing Patterns
  • Unit: Small tests covering individual functions.
  • Integration: Integrated modules(combined functionality).
  • Functional: Focused on end result of larger actions.
  • Acceptance: Typically client-side, end-to-end testing.

JavaScript Testing Frameworks
  • jasmine
  • karma
  • expresso
  • mocha
  • node-tap
  • intern
  • node-unit
  • vows
  • node-qunit
  • More...

MOCHA 
http://mochajs.org/

Mocha is used to unit test nodeJS applications.

MOCHA Setup

-install mocha as a development dependency for project


-Update package.json


Asynchronous Testing


Basic Tests and Assertions

assert.equal: check more or less equal
assert.strictEqual: exactly equal in type and value


Running the Test



Assertion Methods
  • equal: Asserts non-strict equality (==) of actual and expected.
  • strictEqual: Asserts strict equality (===) of actual and expected.
  • deepEqual: Asserts that actual is deeply equal to expected.
  • isOk: Asserts that object is truthy. 
  • isNull: Asserts that value is null.
  • isTrue: Asserts that value is true.
  • fail: Throw a failure. Node.js assert module-compatible.
  • match: Asserts that value matches the regular expression regexp.
  • throws: Asserts that function will throw an error that is an instance of constructor, or alternately that it will throw an error with message matching regexp.

Testing Tips
  • Keep your modules small.
  • Be sure to test success AND error cases.
  • Test expected AND unexpected data.
  • Don't forget that modules are cached.

Testing with Mocks

  • Use mocha to test javascript code. Invoke the command mocha to start the tests.
  • Use chai for assertions.
  • If we find any problematic code then use sinon to spy or stub them to see/ control the behaviour of the functions.
  • Use nock to mock servers to prevent making any http calls to the outside environment.
  • Use rewire to set/ get variables/external dependencies in the javascript file.
  • Use istanbul to calculate the code coverage done by these unit tests.

12 April 2017

Spring Framework



Introduction
  • Rod Johnson started SpringSource company and developed Spring framework for Apache 2.0 in June 2003.
  • The Spring Framework is an open source application framework and inversion of control container for the Java platform.
  • It provides comprehensive infrastructure support or developing Java applications.
  • Spring handles the infrastructure so you can focus on your application.

Why Spring?
  • Spring is a framework like Struts and Hibernate.
  • Spring is famous in the industry.
  • Spring and Hibernate are extremely popular and widely used in the industry.
  • Spring framework is lightweight as it does not involve installation, start and stop activities associated with a container.

Java Frameworks

In Java technology there are so many frameworks that helps the programmers to build complex applications easily.

You can choose these frameworks for building your applications.
  • Hibernate: Database access mechanism
  • Struts: Web layer
  • EJBs: Services like transactions, security, and messaging
  • Log4J: Logging
  • More...

-Java Frameworks problem:
  • Applications that use these varied frameworks and services become difficult to maintain as it grows including testing.

-Applications need:
  • Applications that use a number of frameworks and the services have to remain maintainable.
  • Codes should be loosely coupled with the frameworks so that testing and reusability become easy.

-Spring Framework advantages:
  • Spring Framework provides a light-weight solution to develop maintainable and reusable enterprise applications.
  • It provides very simple and rich facilities to integrate various frameworks, technologies, and services in the application.

-To back up its attack on Java complexity, Spring employs four key strategies:
  • Lightweight and minimally invasive development with Plain Old Java Objects(POJOs).
  • Loose coupling through dependency injection and interface orientation.
  • Declarative programming through aspects and common conventions.
  • Boilerplate reduction through aspects and templates.


Spring Featured/Modules
  • Inversion of Control: Done through Dependency Injection.
  • Aspect Oriented Programming: Enabling implementing cross-cutting concerns.
  • Data Access: Works with JDBC and Hibernate.
  • Model View Controller: Provide MVC Support through servlets and struts. This is web framework.
  • Remote Access Framework: Supports remote access through RMI, web service through SOAP and REST.
  • Remote Management: JMX is the technology that is used to manage the system objects and devices like printers.
  • Messaging: Uses JMS to communicate through Message Queues.
  • Testing: Supports classes for writing unit test cases and integration test cases.
  • Convention over Configuration: Spring Roo is another framework which eases the development. This is Rapid Application Development tool.
  • Authentication and Authorization: This is provided by Spring Security module.

Spring Architecture


Architecture has 5 layers:
  • Data Access/Integration
  • Web
  • Aspect
  • Core
  • Test

Data Access/Integration has JDBC, ORM, OXM, JMS and Transaction modules.
  • JDBC: JDBC code can be done using JDBC Template.
  • ORM: Can develop code using ORM tools like Hibernate, iBatis etc.
  • OXM: OXM stands for Object to XML Mapping. It supports technologies like JAB.
  • JMS: Can send JMS operations in this module.
  • Transaction: Supports both Programmatic and Declarative Transaction Management. Programmatic is handling in the program and declarative is handling either as part of Annotation or using XML.

Web layer has WebSocket, Servlet, Web, Portlet modules.
  • WebSocket: This module provides support for WebSocket-based, two-way communication between the client and the server in web applications.
  • Servlet: Used for servlets.
  • Web: This module will initialize the IOC Container using servlet listener.
  • Portlet: This module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module.

Aspect and Test layer
  • AOP: Used for Aspect Oriented Programming. This will allow to define interceptors and point-cuts to separate them as required.
  • Aspects: This module helps to integrate with AspectJ. AspectJ is another AOP framework.
  • Instrumentation: Instrumentation is the ability to monitor the level of products performance, to diagnose the errors and write the trace information.
  • Messaging: The Messaging module provides support for STOMP as the WebSocket sub-protocol to use in applications. It also supports an annotation programming model for routing and processing STOMP messages from WebSocket clients.
  • Test: Supports to test the spring modules using Junit framework.

Core layer has Beans, Core, Context, SpEL modules.
  • Beans: It implements beanfactory through factory pattern.
  • Core: It has DI and IOC features.
  • Context: Used for ApplicationContext. It uses Core and Beans module.
  • SpEL: This module provides a powerful expression language for querying and manipulating an object graph at runtime.