FeatureRequest: I wish there is a SoftAssertions.assertThat().isNotNullAndSatisfies( Consumer )
- Lingua principale
- Java
- Stelle
- 2.8k
- Fork
- 788
- Merge medio
- 14h 57m
- PR unite (30g)
- 36
Descrizione
#### Summary
When I want to verify fields in a nested object structure, SoftAssertions are cool, because they print out everything and do not stop on the first error.
However, when i access a field that can be null and want to go deeper, I need to write the field access twice or have a variable and "if". Non-fluent calls with repeating symbols:
````java
var something = obj.getSomething();
softly.assertThat( something ).isNotNull();
if( something != null ){
softly.assertthat(something.other()) ...; // (1)
}
... more tests on obj ...
````
The "if" is to avoid, when "something" is null, that it throws NPE in (1) and stops printing the remaining stuff.
The normal satisfies() does not check for isNotNull(), and even when .isNotNull() is chained with satisfies() as the consumer is called. Hence the access will throw NPE.
#### Example
A workaround:
````java
public static Consumer notNullFilter(final Consumer consumer) {
return t -> {
if (t != null) {
consumer.accept(t);
}
};
}
softly.assertThat( obj.getSomething() )
.isNotNull()
.satisfies( notNullFilter( something -> {
softly.assertThat(something.other())
.isEqualTo("x12");
}));
softly.assertThat("asd")
.isEqualTo("xxx");
````
That is near to what I want, but if the .isNotNull() call is forgotten, the block is silently not tested.
So I wish there would be a isNotNullAndSatisfies( Consumer ), which does the isNotNull() and only calls the consumer if the object is non-null:
````java
softly.assertThat( obj.getSomething() )
.isNotNullAndSatisfies( something -> {
softly.assertThat(something.other())
.isEqualTo("x12");
});
softly.assertThat("asd")
.isEqualTo("xxx");
````
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Parti dall’API SoftAssertions.assertThat e confronta il comportamento esistente di isNotNull() e satisfies(Consumer). Aggiungi la copertura per il metodo fluent richiesto usando l’esempio di oggetto nullable annidato dell’issue. Il lavoro è completato quando il consumer viene eseguito solo per i valori non null, i valori null vengono segnalati e le soft assertions successive continuano a essere eseguite.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- java
- Ambito
- testing
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 38/100