Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine porta Logan in un'avventura inedita, violenta e fortemente narrativa, costruita attorno alla sua natura di combattente e al difficile rapporto con il proprio passato. Insomniac Games punta su combattimenti spettacolari, progressione e personalizzazione, inserendo l'azione in un mondo segnato dalla persecuzione dei mutanti. Un viaggio intenso, che alterna mattanza, esplorazione e momenti sorprendentemente emotivi.
DJI Romo 2: tante novità lo rendono un robot completo
DJI Romo 2: tante novità lo rendono un robot completo
Romo 2 è la seconda generazione di robot lavapavimenti di DJI, un modello che si caratterizza per la precisione nel sistema di navigazione e per il funzionamento particolarmente silenzioso. Con le modifiche introdotte in questa seconda versione, e un posizionamento di prezzo più allineato alla concorrenza, rappresenta una valida alternativa sul mercato delle soluzioni di pulizia domestica
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED
Il primo Sony con retroilluminazione True RGB alla prova del banco di misura e dei contenuti: luminanza enorme, colori accurati in HDR e un antiriflesso molto efficace. I limiti sono due sole HDMI 2.1 e il blooming fuori asse
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 09-11-2011, 16:49   #1
e-commerce84
Senior Member
 
Iscritto dal: Feb 2009
Messaggi: 700
[Java] Problema con JUnit, errori vari :-( Un aiutino?

Ciao,
sono nella seguente situazione: ho una classe chiamata Addresses che contiene le informazioni relative ad un indirizzo.

Codice:
package com.sogeit.fids.model.domain;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.NotNull;

public class Addresses implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int addressesPk, contactFk;
	
	@NotNull(message = "may not be null")
	private String addressText;
	
	private String addressNumber, city, countryCode2, districtCode, zipCode, latitude, longitude;
	private boolean isPrimary;
	private Date creationDate, lastUpdate;
	
	private int createdBy;
	private int modifiedBy;
	
	public Addresses(int addressesPk, int contactFk){
		this.addressesPk = addressesPk;
		this.contactFk = contactFk;
	}

	// GETTER PK E FK:
	public int getAddressesPk() {
		return addressesPk;
	}

	public int getContactFk() {
		return contactFk;
	}

	// GETTER E SETTER:
	public String getAddressText() {
		return addressText;
	}

	public void setAddressText(String addressText) {
		this.addressText = addressText;
	}

	public String getAddressNumber() {
		return addressNumber;
	}

	public void setAddressNumber(String addressNumber) {
		this.addressNumber = addressNumber;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountryCode2() {
		return countryCode2;
	}

	public void setCountryCode2(String countryCode2) {
		this.countryCode2 = countryCode2;
	}

	public String getDistrictCode() {
		return districtCode;
	}

	public void setDistrictCode(String districtCode) {
		this.districtCode = districtCode;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public boolean getIsPrimary() {
		return isPrimary;
	}

	public void setIsPrimary(boolean isPrimary) {
		this.isPrimary = isPrimary;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public int getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(int createdBy) {
		this.createdBy = createdBy;
	}

	public int getModifiedBy() {
		return modifiedBy;
	}

	public void setModifiedBy(int modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Addresses [addressesPk=" + addressesPk + ", contactFk="
				+ contactFk + ", addressText=" + addressText
				+ ", addressNumber=" + addressNumber + ", city=" + city
				+ ", countryCode2=" + countryCode2 + ", districtCode="
				+ districtCode + ", zipCode=" + zipCode + ", latitude="
				+ latitude + ", longitude=" + longitude + ", isPrimary="
				+ isPrimary + ", creationDate=" + creationDate
				+ ", lastUpdate=" + lastUpdate + ", createdBy=" + createdBy
				+ ", modifiedBy=" + modifiedBy + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((addressNumber == null) ? 0 : addressNumber.hashCode());
		result = prime * result
				+ ((addressText == null) ? 0 : addressText.hashCode());
		result = prime * result + addressesPk;
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result + contactFk;
		result = prime * result
				+ ((countryCode2 == null) ? 0 : countryCode2.hashCode());
		result = prime * result + createdBy;
		result = prime * result
				+ ((creationDate == null) ? 0 : creationDate.hashCode());
		result = prime * result
				+ ((districtCode == null) ? 0 : districtCode.hashCode());
		result = prime * result + (isPrimary ? 1231 : 1237);
		result = prime * result
				+ ((lastUpdate == null) ? 0 : lastUpdate.hashCode());
		result = prime * result
				+ ((latitude == null) ? 0 : latitude.hashCode());
		result = prime * result
				+ ((longitude == null) ? 0 : longitude.hashCode());
		result = prime * result + modifiedBy;
		result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Addresses))
			return false;
		Addresses other = (Addresses) obj;
		if (addressNumber == null) {
			if (other.addressNumber != null)
				return false;
		} else if (!addressNumber.equals(other.addressNumber))
			return false;
		if (addressText == null) {
			if (other.addressText != null)
				return false;
		} else if (!addressText.equals(other.addressText))
			return false;
		if (addressesPk != other.addressesPk)
			return false;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (contactFk != other.contactFk)
			return false;
		if (countryCode2 == null) {
			if (other.countryCode2 != null)
				return false;
		} else if (!countryCode2.equals(other.countryCode2))
			return false;
		if (createdBy != other.createdBy)
			return false;
		if (creationDate == null) {
			if (other.creationDate != null)
				return false;
		} else if (!creationDate.equals(other.creationDate))
			return false;
		if (districtCode == null) {
			if (other.districtCode != null)
				return false;
		} else if (!districtCode.equals(other.districtCode))
			return false;
		if (isPrimary != other.isPrimary)
			return false;
		if (lastUpdate == null) {
			if (other.lastUpdate != null)
				return false;
		} else if (!lastUpdate.equals(other.lastUpdate))
			return false;
		if (latitude == null) {
			if (other.latitude != null)
				return false;
		} else if (!latitude.equals(other.latitude))
			return false;
		if (longitude == null) {
			if (other.longitude != null)
				return false;
		} else if (!longitude.equals(other.longitude))
			return false;
		if (modifiedBy != other.modifiedBy)
			return false;
		if (zipCode == null) {
			if (other.zipCode != null)
				return false;
		} else if (!zipCode.equals(other.zipCode))
			return false;
		return true;
	}
	

}
Come potete notare tale bean contiene l'annotation @NotNull per il campo addressesText che stà ad indicare che quel campo NON PUO' ESSERE NULLO (annotation per la validazione dei bean: http://docs.jboss.org/hibernate/vali...ngstarted.html )

Poi nella cartella src/test/java ho un package che contiene il seguente test di JUnit:

Codice:
package com.sogeit.fids.model.domain;

import com.sogeit.fids.model.domain.Addresses;
import static org.junit.Assert.*;
 

import java.util.Date;

import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestAddresses {
	
	private static Validator validator;

	
	@Test
	public void test() {
		
		Addresses indirizzo1 = new Addresses(1, 2);
		Addresses indirizzo2 = new Addresses(1, 2);
		
		Date data = new Date(2011, 11, 4);
		
		// indirizzo1:
				indirizzo1.setAddressNumber("Indirizzo");
				indirizzo1.setAddressText("Testo");
				indirizzo1.setCity("Roma");
				indirizzo1.setCountryCode2("countrycode");
				indirizzo1.setCreatedBy(5);
				indirizzo1.setCreationDate(data);
				indirizzo1.setDistrictCode("districtCode");
				indirizzo1.setLastUpdate(data);
				indirizzo1.setLatitude("33");
				indirizzo1.setLongitude("21");
				indirizzo1.setModifiedBy(6);
				indirizzo1.setIsPrimary(true);
				indirizzo1.setZipCode("00100");
				
				// indirizzo2 (uguale ad indirizzo1)
				indirizzo2.setAddressNumber("Indirizzo");
				indirizzo2.setAddressText("Testo");
				indirizzo2.setCity("Roma");
				indirizzo2.setCountryCode2("countrycode");
				indirizzo2.setCreatedBy(5);
				indirizzo2.setCreationDate(data);
				indirizzo2.setDistrictCode("districtCode");
				indirizzo2.setLastUpdate(data);
				indirizzo2.setLatitude("33");
				indirizzo2.setLongitude("21");
				indirizzo2.setModifiedBy(6);
				indirizzo2.setIsPrimary(true);
				indirizzo2.setZipCode("00100");
		
				assertEquals( indirizzo1.hashCode(), indirizzo2.hashCode());
	}
	
	
	@BeforeClass
	public static void setUp() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}
	
	@Test
	public void addressTextIsNull(){
		Addresses indirizzo = new Addresses(5, 10);
		
		Date data = new Date(2011, 11, 4);
		
		// indirizzo1:
		indirizzo.setAddressNumber("Indirizzo");
		indirizzo.setAddressText("Testo");
		indirizzo.setCity("Roma");
		indirizzo.setCountryCode2("countrycode");
		indirizzo.setCreatedBy(5);
		indirizzo.setCreationDate(data);
		indirizzo.setDistrictCode("districtCode");
		indirizzo.setLastUpdate(data);
		indirizzo.setLatitude("33");
		indirizzo.setLongitude("21");
		indirizzo.setModifiedBy(6);
		indirizzo.setIsPrimary(true);
		indirizzo.setZipCode("00100");
		
		Set<ConstraintViolation<Addresses>> constraintViolations = validator.validate(indirizzo);
		
		assertEquals( 1, constraintViolations.size() );
		assertEquals( "may not be null", constraintViolations.iterator().next().getMessage() );
	}
	
}
Tale classe contiene 2 metodi annotati con l'annotation @Test che rappresentano 2 test di JUnit

Se lascio solo il primo test (commentando tutto ciò che non è il metodo chiamato test() ) funziona perfettamente e JUnit mi avverte che il test è stato superato con la classica barra verde

Se aggiungo anche i 2 metodi: setUp() e addressTextIsNull() (lo unit test vero e proprio che dovrebbe testare la validazione del bean relativamente al valore del campo addressText dell'oggetto indirizzo) quando dentro Eclipse faccio Run As JUnit Test NON FUNZIONA...

Non mi appare ne la barra verde che dice che il test è stato passato, ne la barra rossa che dice che il test non è stato passato ma mi viene dato il seguente messaggio di errore:

Codice:
TestAddresses
com.sogeit.fids.model.domain.TestAddresses
javax.validation.ValidationException: Unable to find a default provider

	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)

	at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)

	at com.sogeit.fids.model.domain.TestAddresses.setUp(TestAddresses.java:67)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

	at java.lang.reflect.Method.invoke(Method.java:597)

	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Come mai? Da cosa potrebbe dipendere? Come risolvere?

Grazie mille
Andrea
e-commerce84 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa Marvel's Wolverine, la recensione: Logan torna p...
DJI Romo 2: tante novità lo rendono un robot completo DJI Romo 2: tante novità lo rendono un ro...
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED Sony Bravia 9 II: il True RGB alla prova, dove l...
Geely EX5, un mese al volante: il SUV elettrico cinese che ci ha sorpreso (quasi) senza riserve Geely EX5, un mese al volante: il SUV elettrico ...
Mova Z70 Ultra Roller Complete: motore potente, rullo di lavaggio e l'IA a guidare Mova Z70 Ultra Roller Complete: motore potente, ...
Airbus realizzerà altri 229 satel...
Arianespace lancerà altri satelli...
Eutelsat ha annunciato di aver affidato ...
PLD Space ha annunciato il nuovo razzo s...
CMF Clip Pro, tanta autonomia e design u...
Tre leggende del rally sbarcano in Asset...
iPhone 18 Pro Max, il chip A20 Pro mostr...
Samsung porterà il Privacy Displa...
WINDTRE BUSINESS trasforma la rete mobil...
Anthropic ammette un quarto caso di acce...
Mazda CX-6e, il primo test drive: quando...
La serie HONOR Magic 9 arriva a fine set...
Anker MagGo Power Bank 2 Pro arriva in I...
iRobot rilancia con Roomba Duo, il proto...
#linux su IRC è tornato dopo anni di sil...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 21:44.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v