Upload
diego-pereira
View
333
Download
1
Embed Size (px)
Citation preview
IC
– Integração Contínua
“Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.”
http://www.martinfowler.com/articles/continuousIntegration.html
IC
– Dependency Injection• Design Pattern• Reduzir acoplamento• Vários tipos (constructor injection, setter injection,
interface injection)
IC
– Dependency Injection
class HardcodedWorkflow{
public void execute(){
// do LOTS of workflow stuff...
FileSenderService service = new FileSenderService(fileName, "This is a message");
service.send();
// do LOTS more workflow stuff...
}}
IC
– Dependency Injection
class FileSenderService implements ISenderService{
private String message;private File file;
public FileSenderService(String fileName, String message){
this.parameter = message;this.file = File.open(fileName);
}
public void send(){
file.write(message);}
}
IC
– Dependency Injection
class EmailSenderService implements ISenderService{
...
public void send(){
smtpLib.send(emailAddress, message);}
}
IC
– Dependency Injection
class HardcodedWorkflow{
public void execute(int delivery){
// do LOTS of workflow stuff...
ISenderService service;if(delivery == MsgDelivery.FILE) {
service = new FileSenderService("This is a message");} else if(delivery == MsgDelivery.EMAIL) {
service = new EmailSenderService("This is a slightly different message");}
service.send();
// do LOTS more workflow stuff...
}}
IC
– Dependency Injection
...if(delivery == MsgDelivery.FILE) {
service = new FileSenderService("This is a message"); } else if(delivery == MsgDelivery.EMAIL) {
service = new EmailSenderService(userAddress, "This is a slightly different message"");} else if(delivery == MsgDelivery.ADMINISTRATOR_EMAIL) {
service = new EmailSenderService(adminAddress, "SUDO this is a message!!!");} else if(delivery == MsgDelivery.ABU_DHABI_BY_AIR_MAIL) {
service = new AbuDhabiAirMailSenderService("Hello, Mama");} else if(delivery == MsgDelivery.DEVNULL) {
service = new DevNullSenderService("Hello, is anybody in there??!?!"); // why do I care?!?!}...
IC
– Dependency Injection To The Rescue!!!
class FlexibleWorkflow{
private ISenderService service;
public FlexibleWorkflow(ISenderService service) // << "LOOSE COUPLING"{
this.service = service;}
public void execute(){
// do LOTS of workflow stuff...
this.service.send();
// do LOTS more workflow stuff...
}}
IC
– Dependency Injection To The Rescue!!!
class FileWorkflowController{
public void handleRequest(){
ISenderService service = new FileSenderService("This is a message."); // << "TIGHT COUPLING!!!"
FlexibleWorkflow workflow = new FlexibleWorkflow(service);
workflow.execute();}
}
IC
– Dependency Injection To The Rescue!!!
class EmailWorkflowController{
public void handleRequest(){
ISenderService service = new EmailSenderService(getUserEmail(), "This is a slightly different message.");
FlexibleWorkflow workflow = new FlexibleWorkflow(service);
workflow.execute();}
}
IC
– Testes com Mocks!!!
class FlexibleWorkflowTest extends TestCase{
// millions of other tests...
public void testMessageSend(){
ISenderService mockService = new MockSenderService();
FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);
workflow.execute();
assertTrue(workflow.getResult(), true);
mockService.assertCalled("send");// millions of other assertions...
}}
IC
– Mock Objects• Mocks aren’t Stubs!
http://martinfowler.com/articles/mocksArentStubs.html
IC
– Stubs
ISenderService stubService = new StubService();
FlexibleWorkflow workflow = new FlexibleWorkflow(stubService);workflow.execute();
// nada mais a fazer, o stub nao provê nenhuma informacao
IC
– Mock Objects• Inspecionar comportamento
class FlexibleWorkflowTest extends TestCase{
public void testMessageSend(){
ISenderService mockService = new MockSenderService();
FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);
workflow.execute();
assertTrue(workflow.getResult(), true);
mockService.assertCalled("send");// millions of other assertions...
}}
IC
– Mock Objects• Simulando condições de erro
public void testMessageSendError(){
ISenderService mockService = new MockSenderService();
// mock lanca "SmtpTimeoutException" quando// metodo "send" e chamadomockService.setSideEffect("send",
new SideEffect() { void run () { throw new SmtpTimeoutException(); } });
FlexibleWorkflow workflow = new FlexibleWorkflow(mockService);
try {workflow.execute();
} catch(Exception e) {assertFail();
}}
interface SideEffect {void run();
}
IC
– TDD
“You have probably heard a few talks or read a few articles where test driven development is depicted as a magic unicorn that watches over your software and makes everybody happy.”
IC
– TDD
“ Well, about 18.000 lines of ‘magic unicorn’ code later, I'd like to put things into perspective.”
IC
– TDD
“But do you know what sucks more? The liability that comes without those tests.”
http://debuggable.com/posts/test-driven-development-at-transloadit:4cc892a7-b9fc-4d65-bb0c-1b27cbdd56cb
IC
– TDD• Implementar mínimo necessário para o teste FALHAR.
class Math{
public int sum(int a, int b) {
throw new NotImplementedException();}
}
class MathTest extends TestCase{
public void testSum(){
Math m = new Math();assertEquals(m.sum(1,2), 3);
}}
IC
– TDD• Vantagens
– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.
IC
– TDD• Vantagens
– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.
IC
– TDD• Vantagens
– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.– Design gera interfaces (de código) mais “amigáveis”.
IC
– TDD• Vantagens
– Código é testável “por natureza”.– Ajuda a usar Mocks e Injeção de Dependências.– Cobertura de Código.– Design gera interfaces (de código) mais “amigáveis”.– TDD não tem “ciúminho”.
IC
– TDD• Desvantagens
– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!
– Nada é de graça (No Unicorns)
IC
– TDD• Desvantagens
– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!
– Nada é de graça (No Unicorns)– Código legado
IC
– TDD• Desvantagens
– Na 1a. vez, prepare-se para errar. Muito.» Mas aprender, mais ainda!
– Nada é de graça (No Unicorns)– Código legado– É um “pé no saco”
Conclusão
• There’s no Free Lunch ...• ... and no Silver Bullet!• Baby Steps!• Purismo não leva a lugar nenhum• Ferramentas adequadas