Click here to load reader
Upload
robbie-clutton
View
942
Download
0
Embed Size (px)
Citation preview
Software Engineering
Robbie Clutton
readme.txt
• Started City University 2001• Did industrial placement with Lloyds TSB
2003-2004– Coding Cobol 74, editing code written before I was
born!
• Graduated Software Engineering BEng 2005• Joined BT graduate scheme
readme.txt
• SDK to access BT Web Services in .NET C#• Worked with customers to build prototypes
against BT Web Services• RESTful website using Ruby on Rails• SIP Application Server to support BT Web
Services using Java• IVR Web Service using SIP A/S using Java• Website using PHP
Unit Testing
• Tests a unit of code (e.g. a method)• Doing the simplest thing that could possibly
work• Living documentation• Allows refactoring• Assert upon the correctness of the application
@Testpublic void testHelloWorld(){
// setupHelloWorld helloWorld = new HelloWorld();// actString result = helloWorld.sayHello();// assertassertEquals("Hello", result);
}
package com.iclutton.seday;
public class HelloWorld {
public String sayHello() {return language.sayHello();
}
}
Interfaces
• Benefits unit testing• Makes code cleaner
package com.iclutton.seday.language;public interface Language { String sayHello();}
package com.iclutton.seday.language;
public class English implements Language{
@Overridepublic String sayHello() {
return "Hello";}
}
package com.iclutton.seday.language;
public class French implements Language{
@Overridepublic String sayHello(){
return "Bonjour";}
}
Mocking
• Useful when you want to ‘mock’ out external dependencies– Databases– Web services– Etc
• Useful for simulating error conditions and handling
@Testpublic void testHelloWorldWithMock(){
// setupEnglish english = mock(English.class);when(english.sayHello()).thenReturn("Alright");HelloWorld helloWorld = new HelloWorld();helloWorld.setLanguage(english);// actString result = helloWorld.sayHello();// assertassertEquals("Alright", result);verify(english).sayHello();
}
@Test(expected=IllegalStateException.class)public void testHelloWorldWithMockException(){
// setupEnglish english = mock(English.class);when(english.sayHello()).thenThrow(new RuntimeException());HelloWorld helloWorld = new HelloWorld();helloWorld.setLanguage(english);// acthelloWorld.sayHello();// assert - see exception annotation
}
Dependency Injection
• We’ve already seen dependency injection• Now let’s look at Spring, which supports– Reduces complexities of using interfaces– Great way to configure applications– Promotes the use of singletons– Improves testability
<bean id="language" class="com.iclutton.seday.language.English"/>
<bean id="hello" class="com.iclutton.seday.HelloWorld"><property name="language" ref="language"/>
</bean>
@Testpublic void testSpring(){
// setupApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("hello");// actString result = helloWorld.sayHello();// assertassertEquals("Hello", result);
}
Build Scripts
• Manual processes are more error prone• Build scripts bring consistency and automation• There are many varieties– Ant (Java)– nAnt (.NET)– Make (C/C++)– Rake (Ruby)– etc
Property Driven Builds
• Useful when more that one person becomes involved
• Or deploying to different environments
Source Control
Automating the Build
• Continuous Integration
Logging
• Until you support an application you’ve built you will never know the value of logging
# Root categorylog4j.rootLogger=DEBUG, stdoutlog4j.logger.com.iclutton.seday=DEBUG
# CONSOLElog4j.appender.stdout.Threshold=DEBUGlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %X{stackname}[%t] %-5p [%C{1}.%M() %L] - %m%n
2008-11-18 18:06:39,953 [main] DEBUG [AbstractAutowireCapableBeanFactory$1.run() 411] - Finished creating instance of bean 'hello'2008-11-18 18:06:39,953 [main] DEBUG [AbstractApplicationContext.publishEvent() 273] - Publishing event in context [org.springframework.context.support.ClassPathXmlApplicationContext@1e845c2]: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@1e845c2: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1e845c2]; startup date [Tue Nov 18 18:06:39 GMT 2008]; root of context hierarchy]2008-11-18 18:06:39,953 [main] DEBUG [AbstractBeanFactory.doGetBean() 214] - Returning cached instance of singleton bean 'hello'
Q&A