MockitoJUnitRunner.class?

Pessoal, é possível testar uma entidade ?

tipo :

[code]package br.com.inss;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@SequenceGenerator(name = “web_pessoa_seq”, sequenceName = “web_pessoa_seq”)
@Table(name = “web_pessoa”, uniqueConstraints = {
@UniqueConstraint(columnNames = “cpf”),
@UniqueConstraint(columnNames = “cnpj”) })
public class Pessoa extends InssEntity {

private static final long serialVersionUID = 1L;

private Long id;
private String nome;
private String cpf;
private String cnpj;

public Pessoa() {
	super();
}

public Pessoa(Long id, String nome) {
	super();
	this.id = id;
	this.nome = nome;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "web_pessoa_seq")
public Long getId() {
	return id;
}

public void setId(Long id) {
	this.id = id;
}

@NotNull(message = "br.com.inss.pessoa.nome.notNull")
@Size(min = 2, message = "br.com.inss.pessoa.nome.size")
public String getNome() {
	return nome;
}

public void setNome(String nome) {
	this.nome = nome;
}

public String getCpf() {
	return cpf;
}

public void setCpf(String cpf) {
	this.cpf = cpf;
}

public String getCnpj() {
	return cnpj;
}

public void setCnpj(String cnpj) {
	this.cnpj = cnpj;
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((id == null) ? 0 : id.hashCode());
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Pessoa other = (Pessoa) obj;
	if (id == null) {
		if (other.id != null)
			return false;
	} else if (!id.equals(other.id))
		return false;
	return true;
}

}
[/code]


import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class TestPessoaTest {
	@Mock
	private String cnpj;

	@Mock
	private String cpf;

	@Mock
	private Long id;

	@Mock
	private String nome;

	@Mock
	private Long versao;
	private Pessoa pessoa;

	@Before
	public void createPessoa() throws Exception {
		pessoa = new Pessoa(id, nome);
		pessoa.setCnpj(cnpj);
		pessoa.setCpf(cpf);
		pessoa.setVersao(versao);
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test
	public final void testHashCode() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testPessoa() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testPessoaLongString() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testGetId() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testSetId() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testGetNome() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testSetNome() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testGetCpf() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testSetCpf() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testGetCnpj() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testSetCnpj() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testEqualsObject() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testHapvidaEntity() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testGetVersao() {
		fail("Not yet implemented"); // TODO
	}

	@Test
	public final void testSetVersao() {
		fail("Not yet implemented"); // TODO
	}

}

Sim. Porquê não seria possível ? E pergunta que não quer calar, qual a necessidade de você testar uma entidade ? Só para cobrir a % de cobertura ? :slight_smile:

Creio que você também não precise de mockito para este simples teste.

Ex:

@Test
public void testSetCpf() {
    Pessoa pessoa = new Pessoa();
    String umCpf = "12345";
    pessoa.setCpf(umCpf);
    assertEquals(umCpf, pessoa.getCpf());
}

[]'s

Acho que dá poderia-se testar a entidade se os campos estão preenchidos ou o tipo de dados.
Quem tem mais experiência em tdd pode explicar melhor eu acho.
Obrigado

WRYEL Boa Tarde !

Na verdade, fui simplista no exemplo…
Mas você ta correto não precisa do mockito…

Ajudou muito !

Obrigado !