Mockito

Pessoal estou usando MOCKITO, mas estou achando muito trabalhoso, alguém tem alguma sugestão de algo mais rápido de fazer !?


import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;

import junit.framework.Assert;

import org.junit.Test;
import org.mockito.Mockito;

public class TextConverterTest {

    private TextConverter textConverter;

    public TextConverterTest() {
        textConverter = new TextConverter();
    }

    @Test
    public void testSuccessGetAsObjectCPF() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "cpf");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertNotNull(textConverter.getAsObject(null, uiComponent, "111.111.111-11"));
    }

    @Test
    public void testSuccessGetAsObjectMoeda() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "moeda");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertNotNull(textConverter.getAsObject(null, uiComponent, "R$ 1254,10"));
    }

    @Test
    public void testFailGetAsObjectException() {
        FacesContext facesContext = Mockito.mock(FacesContext.class);
        Application application = Mockito.mock(Application.class);
        Mockito.when(application.getMessageBundle()).thenReturn("messages_sesacore");
        Mockito.when(facesContext.getApplication()).thenReturn(application);
        UIViewRoot uiViewRoot = Mockito.mock(UIViewRoot.class);
        Mockito.when(uiViewRoot.getLocale()).thenReturn(new Locale("pt_BR"));
        Mockito.when(facesContext.getViewRoot()).thenReturn(uiViewRoot);
        HtmlInputText component = new HtmlInputText();
        component.setLabel("select");
        component.getAttributes().put("type", "cpf");
        try {
            textConverter.getAsObject(null, component, "A");
            Assert.assertTrue(false);
        } catch (Exception e) {
            Assert.assertTrue(true);
        }
    }

    @Test
    public void testGetAsStringCPF() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "cpf");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("111.111.111-11", textConverter.getAsString(null, uiComponent, "11111111111"));
    }

    @Test
    public void testGetAsStringCNPJ() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "cnpj");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("11.111.111/1111-11", textConverter.getAsString(null, uiComponent, "11111111111111"));
    }

    @Test
    public void testGetAsStringCEP() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "cep");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("11111-111", textConverter.getAsString(null, uiComponent, "11111111"));
    }

    @Test
    public void testGetAsStringTelefone() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "telefone");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("(11)1111.1111", textConverter.getAsString(null, uiComponent, "1111111111"));
    }

    @Test
    public void testGetAsStringPorcentagem() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "porcentagem");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("1,50%", textConverter.getAsString(null, uiComponent, "1.5"));
    }

    @Test
    public void testGetAsStringPorcentagemSemVirgula() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "porcentagem");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("1,00%", textConverter.getAsString(null, uiComponent, "1"));
    }

    @Test
    public void testGetAsStringMoeda() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "moeda");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("R$ 1.500,23", textConverter.getAsString(null, uiComponent, "1500.23"));
    }

    @Test
    public void testGetAsStringMoedaSemVirgula() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "moeda");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("R$ 1.500,00", textConverter.getAsString(null, uiComponent, "1500"));
    }

    @Test
    public void testGetAsStringMoedaNull() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "moeda");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("", textConverter.getAsString(null, uiComponent, null));
    }

    @Test
    public void testGetAsStringDecimal() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "decimal");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("1.500,23", textConverter.getAsString(null, uiComponent, "1500.23"));
    }

    @Test
    public void testGetAsStringDecimalSemVirgula() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "decimal");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("1.500,00", textConverter.getAsString(null, uiComponent, "1500"));
    }

    @Test
    public void testGetAsStringDecimalNull() {
        UIComponent uiComponent = Mockito.mock(UIComponent.class);
        Map<String, Object> atributos = new HashMap<String, Object>();
        atributos.put("type", "decimal");
        Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);
        Assert.assertEquals("", textConverter.getAsString(null, uiComponent, null));
    }
}

Uma refatoração simples no seu código poderia melhorar razoavelmente seus testes.
Você poderia criar um método para criar seu mock do UIComponent:

private UIComponent getUIComponentMock(String typeValue) {
    UIComponent uiComponent = Mockito.mock(UIComponent.class);  

    Map<String, Object> atributos = new HashMap<String, Object>();  
    atributos.put("type", typeValue);

    Mockito.when(uiComponent.getAttributes()).thenReturn(atributos);

    return uiComponent;
}

E refatorar seus testes para utilizar esse novo método, por exemplo:

@Test  
public void testSuccessGetAsObjectCPF() {  
    UIComponent uiComponent = getUIComponentMock("cpf");
 
    Assert.assertNotNull(textConverter.getAsObject(null, uiComponent, "111.111.111-11"));  
}

@Test  
public void testSuccessGetAsObjectMoeda() {  
    UIComponent uiComponent = getUIComponentMock("moeda");
 
    Assert.assertNotNull(textConverter.getAsObject(null, uiComponent, "R$ 1254,10"));
}

Lembre-se que seus testes tambem merecem um bom design ao serem escritos :smiley:

uma curiosidade, qual a vantagem que vocês estão tendo em criar testes em cima de componentes? Qual o ganho disso ai? Se amanhã muda o front-end todo codigo de teste é jogado fora?

LPJava Bom Dia !

Para meu projeto, que não ocorre mudanças, é muito válido !

Abraço.