Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh
Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh
realme 16 5G è un nuovo smartphone con sensore Sony IMX 852 da 50MP sul retro e uno specchio selfie fisico integrato nella camera bar, una prima nel segmento di mercato. Batteria da 6550mAh in un corpo da 8,1mm e 183g, certificazione IP69K e ricarica da 45W completano un pacchetto aggressivo per la fascia media, per uno dei prodotti più interessanti del produttore sul piano commerciale
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni
Sono ormai definitive le nuove norme del Codice della Strada per i monopattini elettrici. Non solo targa e assicurazione, le regole sono tante e riguardano diversi aspetti, vi spieghiamo come evitare sanzioni che possono essere salate
DLSS 4.5: con Dynamic Frame Generation e MFG 6X NVIDIA alza la posta
DLSS 4.5: con Dynamic Frame Generation e MFG 6X NVIDIA alza la posta
DLSS 4.5 introduce Dynamic Multi Frame Generation e MFG 6X, permettendo fino a cinque frame generati per ogni frame renderizzato. I test su Cyberpunk 2077 e 007 First Light mostrano forti incrementi di FPS e riduzione della latenza su RTX 5090 Laptop. Migliorano fluidità, stabilità e qualità visiva.
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


Recensione realme 16 5G: lo smartphone con Selfie Mirror ha una batteria da 6550mAh Recensione realme 16 5G: lo smartphone con Selfi...
Come rispettare tutte le nuove regole per i monopattini elettrici? La guida per non rischiare sanzioni Come rispettare tutte le nuove regole per i mono...
DLSS 4.5: con Dynamic Frame Generation e MFG 6X NVIDIA alza la posta DLSS 4.5: con Dynamic Frame Generation e MFG 6X ...
Plaud NotePin S, il registratore IA si fa indossabile (ma è facile da perdere) Plaud NotePin S, il registratore IA si fa indoss...
Redmi Watch 6 in prova: lo smartwatch con ampio display da 2000 nit a meno di 100 euro Redmi Watch 6 in prova: lo smartwatch con ampio ...
Vertiv: data center, la corsa dell’IA sp...
Siri non diventerà la tua fidanzata virt...
Prezzi in crescita del 200% e forniture ...
Ancora non lo sai, ma il 'botsitting' ti...
L'IA costringe a velocizzare lo sviluppo...
NVIDIA l'ha tenuta nascosta per anni: sp...
Più smartphone, meno fiki fiki: l...
CrossOver taglia i ponti con Intel: il t...
Ancora doppio sconto sulla Engwe pi&ugra...
Pezzotto, scatta maxi sequestro da 650.0...
Ecco come acquistare EA FC 26 a 18€: gio...
Google: un nuovo speaker smart da 99 dol...
Un tessuto innovativo può raccogl...
Uno YouTuber vuole denunciare Samsung: l...
Pokémon Go utilizzato per addestrare i d...
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: 14:01.


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