Loyalty Hybrid Project – Testing Approach – Why Worry About Testing??



Loyalty Hybrid Project – Testing Approach – Why Worry About Testing??

0 0


testing-presentation


On Github nllarson / testing-presentation

Loyalty Hybrid Project

Testing Approach

By Nick Larson

About Me

Why Worry About Testing??

We needed people to run the tests!

Easy To Run

  • No Setup - should run from checkout
  • 1 Click **

Fast

  • Fast enough to easily run before a commit.

Repeatable

  • Known state before each test.
  • One test should not depend on another.
  • Cleanup if needed.

Portable

  • Any machine
  • Self Contained

Code To Be Tested...

Show Foo.javaShow FooMgr.java

Unit vs Integration

Unit Testing

Wikipedia: A method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.

Nick Larson: Testing a single class, method, piece of code (Unit). Without any real dependencies.

Mocking

  • Alot of code does depend on external code. (Manager, Service, DAO, etc)
  • Mocking / Stubbing allows us to specify what we want returned from these dependencies without worrying about how the dependency is configured, written, etc.

Mockito

http://code.google.com/p/mockito/

Mockito offers a simple and intuitive approach: you ask questions about interactions after execution. Using mockito, you can verify what you want.

Mockito Features

  • Mocks concrete classes as well as interfaces
  • Little annotation syntax sugar - @Mock
  • Verification errors are clean
  • Allows flexible verification in order (e.g: verify in order what you want, not every single interaction)
  • Flexible verification or stubbing using argument matchers (anyObject(), anyString() or refEq() for reflection-based equality matching)

Simple Examples

when(foo.bar(0)).thenReturn(99);

when(foo.bar(0)).thenReturn(99).thenReturn(77);

when(foo.bar(0)).thenThrow(new Exception());

when(foo.bar(anyInteger())).thenReturn(99);

when(foo.baz(any(User.class))).thenReturn(99);

verify(foo, times(3)).bat(anyString());

verify(foo, atLeastOnce()).bat(anyString());

verify(foo, atMost(3)).bat(anyString());

Not Such A Simple Example

when(foo.loadUser(anyString())).thenAnswer(new Answer()
{
	public User answer(InvocationOnMock invocation) 
		throws Throwable
	{
		User user = null;
		String userId;

		userId = (String) invocation.getArguments()[0];
		
		if (!userId.equals("abc123"))
		{
			user = new User();
			user.setId(userId);
			user.setName("Test User");
		}

		return user;
	}
});
						
This allows some logic to be thrown into the stubs. In this example we are wanting to only 'load' a user if the id is NOT == 'abc123'.

Let's Code....

Integration Testing

Wikipedia: Individual software modules are combined and tested as a group. It occurs after unit testing. Integration testing takes as its input modules that have been unit tested, groups them in larger aggregates

Nick Larson: Yeah, what they said! ^^^^^^

Integration Testing Pain Points

  • Spring Container
  • Database
  • App Server (Functional Testing)
  • Spring has good testing support. We make use of the AbstractTransactionalTestNGSpringContextTests
  • Functional Testing is a 3rd phase of testing that we haven't taken on. The difficulties around functional testing include cross browser support. Tools like selenium are close, but not perfect. We all know the headache around testing in IE7, imagine having to write code to test actually do the testing on individual browsers. And it can be tougher to verfiy data in the functional phase. You can only really verify what is shown to you on the screen.

H2

http://www.h2database.com/

H2

  • Very fast, open source, JDBC API
  • Embedded and server modes (In-memory databases)
  • Small footprint: around 1 MB jar file size
  • Build Datasets that live in your project.

More Code....

How Can We Make It Better?

  • Ant Target
  • Continuous Integration

Questions????

References