Domanda

Qui di seguito è una classe che ho scritto in un'applicazione web che sto costruendo uso di Java, Google App Engine.Ho scritto Unit Test utilizzando TestNG e tutte le prove superate.Ho quindi eseguire EclEmma in Eclipse per vedere la copertura dei test sul mio codice.Tutte le funzioni del 100% di copertura, ma il file in mostra circa il 27% di copertura.Dove si trova il 73% scoperto di codice provenienti da?

Qualcuno mi può aiutare a capire come EclEmma funziona e perché io sono sempre la discrepanza nei numeri?

package com.skaxo.sports.models;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType= IdentityType.APPLICATION)
public class Account {

    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String userId;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

    @Persistent
    private String email;

    @Persistent
    private boolean termsOfService;

    @Persistent
    private boolean systemEmails;

    public Account() {}

    public Account(String firstName, String lastName, String email) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public Account(String userId) {
        super();
        this.userId = userId;
    }

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

    public Long getId() {
        return id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean acceptedTermsOfService() {
        return termsOfService;
    }

    public void setTermsOfService(boolean termsOfService) {
        this.termsOfService = termsOfService;
    }

    public boolean acceptedSystemEmails() {
        return systemEmails;
    }

    public void setSystemEmails(boolean systemEmails) {
        this.systemEmails = systemEmails;
    }
}

Di seguito è il codice di prova per la classe precedente.

package com.skaxo.sports.models;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AccountTest {

    @Test
    public void testId() {
        Account a = new Account();
        a.setId(1L);
        assertEquals((Long) 1L, a.getId(), "ID");
        a.setId(3L);
        assertNotNull(a.getId(), "The ID is set to null.");
    }

    @Test
    public void testUserId() {
        Account a = new Account();
        a.setUserId("123456ABC");
        assertEquals(a.getUserId(), "123456ABC", "User ID incorrect.");
        a = new Account("123456ABC");
        assertEquals(a.getUserId(), "123456ABC", "User ID incorrect.");
    }

    @Test
    public void testFirstName() {
        Account a = new Account("Test", "User", "test@example.com");
        assertEquals(a.getFirstName(), "Test", 
                "User first name not equal to 'Test'.");
        a.setFirstName("John");
        assertEquals(a.getFirstName(), "John", 
                "User first name not equal to 'John'.");
    }

    @Test
    public void testLastName() {
        Account a = new Account("Test", "User", "test@example.com");
        assertEquals(a.getLastName(), "User",
                "User last name not equal to 'User'.");
        a.setLastName("Doe");
        assertEquals(a.getLastName(), "Doe", 
                "User last name not equal to 'Doe'.");
    }

    @Test
    public void testEmail() {
        Account a = new Account("Test", "User", "test@example.com");
        assertEquals(a.getEmail(), "test@example.com", 
                "User email not equal to 'test@example.com'.");
        a.setEmail("john@example.com");
        assertEquals(a.getEmail(), "john@example.com", 
                "User email not equal to 'john@example.com'.");
    }

    @Test
    public void testAcceptedTermsOfService() {
        Account a = new Account();
        a.setTermsOfService(true);
        assertTrue(a.acceptedTermsOfService(),
                "Accepted Terms of Service not true.");
        a.setTermsOfService(false);
        assertFalse(a.acceptedTermsOfService(),
                "Accepted Terms of Service not false.");
    }

    @Test
    public void testAcceptedSystemEmails() {
        Account a = new Account();
        a.setSystemEmails(true);
        assertTrue(a.acceptedSystemEmails(), "System Emails is not true.");
        a.setSystemEmails(false);
        assertFalse(a.acceptedSystemEmails(), "System Emails is not false.");
    }
}
È stato utile?

Soluzione

Questa è un'ipotesi, ma sulla base di Javadoc PersistenceCapable sembra che la classe è intrecciata con codice aggiuntivo da JDO enhancer, per implementare l'interfaccia.Se questo è il caso, è possibile che il codice aggiuntivo, che non è coperta dai test.Se si rimuove l'annotazione ed eseguire il test di nuovo si fa a vedere la copertura prevista?

Da Javadoc:

L'Implementazione di Riferimento, il JDO Enhancer modifica la classe a implementare PersistenceCapable prima di caricare la classe in un ambiente di runtime.Il Riferimento Enhancer aggiunge anche il codice per implementare i metodi definiti da PersistenceCapable.

Si potrebbe anche provare ad usare un decompilatore come JAD per ispezionare la classe compilata per verificare se la classe è, infatti, tessuta con altri metodi, in fase di compilazione (o come pre processo).Di nuovo da Javadoc:

I metodi, in PersistenceCapable interfaccia potrebbe essere generata da pre-elaborazione .un file java, o potrebbe essere generato da uno strumento direttamente.La tecnica esatta per la generazione di metodi, non è specificato da JDO.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top