Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32055 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-07-17 10:36 +0200 |
| Last post | 2015-07-25 17:10 +0200 |
| Articles | 11 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
How to test my @NotEmpty with JUnit Cecil Westerhof <Cecil@decebal.nl> - 2015-07-17 10:36 +0200
Re: How to test my @NotEmpty with JUnit Joerg Meier <joergmmeier@arcor.de> - 2015-07-17 12:28 +0200
Re: How to test my @NotEmpty with JUnit Cecil Westerhof <Cecil@decebal.nl> - 2015-07-17 13:22 +0200
Re: How to test my @NotEmpty with JUnit Cecil Westerhof <Cecil@decebal.nl> - 2015-07-22 11:49 +0200
Re: How to test my @NotEmpty with JUnit Jeff Higgins <jeff@invalid.invalid> - 2015-07-17 07:27 -0400
Re: How to test my @NotEmpty with JUnit Jeff Higgins <jeff@invalid.invalid> - 2015-07-17 07:58 -0400
Re: How to test my @NotEmpty with JUnit Jeff Higgins <jeff@invalid.invalid> - 2015-07-17 09:14 -0400
Re: How to test my @NotEmpty with JUnit Jeff Higgins <jeff@invalid.invalid> - 2015-07-17 10:59 -0400
Re: How to test my @NotEmpty with JUnit Jeff Higgins <jeff@invalid.invalid> - 2015-07-17 11:39 -0400
Re: How to test my @NotEmpty with JUnit Cecil Westerhof <Cecil@decebal.nl> - 2015-07-22 12:09 +0200
Re: How to test my @NotEmpty with JUnit Cecil Westerhof <Cecil@decebal.nl> - 2015-07-25 17:10 +0200
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-07-17 10:36 +0200 |
| Subject | How to test my @NotEmpty with JUnit |
| Message-ID | <87io9jyycu.fsf@Equus.decebal.nl> |
We have to remove almost every framework used in the code. I started
with the @NotEmpty from Hibernate. It was the only class used from
Hibernate, so low hanging fruit we thought.
I wrote the following implementation:
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.ReportAsSingleViolation;
@Documented
@Constraint(validatedBy = {})
@Target({ FIELD })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
public abstract String message() default "Field has to be filled";
}
But it should be tested also of-course. So I wrote the following test:
import org.junit.Test;
class NotEmptyClass {
@NotEmpty
String notEmpty;
NotEmptyClass(String value) {
notEmpty = value;
}
}
public class NotEmptyTest {
@Test
public void firstTest() {
new NotEmptyClass(null);
}
}
I would expect this test not to pass, but it does. So clearly I am
doing something wrong, but I have no idea what. (This is the first
time I really work with annotations.) I did some Googling but until
now to no avail. So if someone could point me to the right direction …
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2015-07-17 12:28 +0200 |
| Message-ID | <1qa7i0ii8kv64.19w4a3yff0hh1$.dlg@40tude.net> |
| In reply to | #32055 |
On Fri, 17 Jul 2015 10:36:33 +0200, Cecil Westerhof wrote: > import javax.validation.Constraint; > [...] > I would expect this test not to pass, but it does. So clearly I am > doing something wrong, but I have no idea what. (This is the first > time I really work with annotations.) I did some Googling but until > now to no avail. So if someone could point me to the right direction … Just to make sure you didn't miss the obvious: are you aware that the javax.validation package needs an external tool such as the Checker framework in order to do anything at all ? Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-07-17 13:22 +0200 |
| Message-ID | <87egk7yqnu.fsf@Equus.decebal.nl> |
| In reply to | #32056 |
On Friday 17 Jul 2015 12:28 CEST, Joerg Meier wrote: > On Fri, 17 Jul 2015 10:36:33 +0200, Cecil Westerhof wrote: > >> import javax.validation.Constraint; > >> [...] > >> I would expect this test not to pass, but it does. So clearly I am >> doing something wrong, but I have no idea what. (This is the first >> time I really work with annotations.) I did some Googling but until >> now to no avail. So if someone could point me to the right >> direction … > > Just to make sure you didn't miss the obvious: are you aware that > the javax.validation package needs an external tool such as the > Checker framework in order to do anything at all ? No I did not. (I am new to this.) Any recommendations what to use and how to implement it? Can it be implemented without creating dependencies? The client wants clean JEE6 code, so they can change frameworks at a moments notice. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-07-22 11:49 +0200 |
| Message-ID | <87vbdco73b.fsf@Equus.decebal.nl> |
| In reply to | #32057 |
On Friday 17 Jul 2015 13:22 CEST, Cecil Westerhof wrote: > On Friday 17 Jul 2015 12:28 CEST, Joerg Meier wrote: > >> On Fri, 17 Jul 2015 10:36:33 +0200, Cecil Westerhof wrote: >> >>> import javax.validation.Constraint; >> >>> [...] >> >>> I would expect this test not to pass, but it does. So clearly I am >>> doing something wrong, but I have no idea what. (This is the first >>> time I really work with annotations.) I did some Googling but >>> until now to no avail. So if someone could point me to the right >>> direction … >> >> Just to make sure you didn't miss the obvious: are you aware that >> the javax.validation package needs an external tool such as the >> Checker framework in order to do anything at all ? > > No I did not. (I am new to this.) Any recommendations what to use > and how to implement it? I managed it. :-D > Can it be implemented without creating dependencies? The client > wants clean JEE6 code, so they can change frameworks at a moments > notice. And I also managed this. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-07-17 07:27 -0400 |
| Message-ID | <moap0a$qil$1@dont-email.me> |
| In reply to | #32055 |
On 07/17/2015 04:36 AM, Cecil Westerhof wrote:
> We have to remove almost every framework used in the code. I started
> with the @NotEmpty from Hibernate. It was the only class used from
> Hibernate, so low hanging fruit we thought.
>
> I wrote the following implementation:
>
> import java.lang.annotation.Documented;
> import static java.lang.annotation.ElementType.FIELD;
> import java.lang.annotation.Retention;
> import static java.lang.annotation.RetentionPolicy.RUNTIME;
> import java.lang.annotation.Target;
>
> import javax.validation.Constraint;
> import javax.validation.constraints.NotNull;
> import javax.validation.constraints.Size;
> import javax.validation.ReportAsSingleViolation;
>
> @Documented
> @Constraint(validatedBy = {})
> @Target({ FIELD })
> @Retention(RUNTIME)
> @ReportAsSingleViolation
> @NotNull
> @Size(min = 1)
> public @interface NotEmpty {
> public abstract String message() default "Field has to be filled";
> }
>
>
> But it should be tested also of-course. So I wrote the following test:
>
> import org.junit.Test;
>
> class NotEmptyClass {
> @NotEmpty
> String notEmpty;
>
> NotEmptyClass(String value) {
> notEmpty = value;
> }
> }
>
> public class NotEmptyTest {
> @Test
> public void firstTest() {
> new NotEmptyClass(null);
> }
> }
>
> I would expect this test not to pass, but it does. So clearly I am
> doing something wrong, but I have no idea what. (This is the first
> time I really work with annotations.) I did some Googling but until
> now to no avail. So if someone could point me to the right direction …
>
As I stated before I'm not a JPA or JavaEE user so this might be
CompleteBS.
I also did some googling "test custom bean validation" and from
what I read I wonder why you don't show a ConstraintValidator class.
And also the JUnit tests I ran across seem to do a good bit of
setup that you don't show.
One link that I picked almost at random.
<http://www.javabeat.net/bean-validation-java-ee-creating-custom-constraints-validations/>
Also the spec seems easy to read. Overwhelming? Well, yep, but...
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-07-17 07:58 -0400 |
| Message-ID | <moaqp3$vv4$1@dont-email.me> |
| In reply to | #32058 |
On 07/17/2015 07:27 AM, Jeff Higgins wrote: > As I stated before I'm not a JPA or JavaEE user so this might be > CompleteBS. Chapter 10. Integrating with other frameworks <http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-integration.html>
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-07-17 09:14 -0400 |
| Message-ID | <moav7e$gal$1@dont-email.me> |
| In reply to | #32058 |
On 07/17/2015 07:27 AM, Jeff Higgins wrote: > As I stated before I'm not a JPA or JavaEE user so this might be > CompleteBS. WYTIWYR : What You Test Is What You Run <http://antoniogoncalves.org/2012/01/16/wytiwyr-what-you-test-is-what-you-run/>
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-07-17 10:59 -0400 |
| Message-ID | <mob5d2$80j$1@dont-email.me> |
| In reply to | #32058 |
On 07/17/2015 07:27 AM, Jeff Higgins wrote: > As I stated before I'm not a JPA or JavaEE user so this might be > CompleteBS. Java Platform, Enterprise Edition 7: Validating a Method with Bean Validation 1.1 <http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BeanValidation/validating_parameters_bean_validation.html> Unit Testing for Java EE http://www.oracle.com/technetwork/articles/java/unittesting-455385.html Integration Testing for Java EE <http://www.oracle.com/technetwork/articles/java/integrationtesting-487452.html>
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-07-17 11:39 -0400 |
| Message-ID | <mob7on$g9f$1@dont-email.me> |
| In reply to | #32058 |
On 07/17/2015 07:27 AM, Jeff Higgins wrote: > As I stated before I'm not a JPA or JavaEE user so this might be > CompleteBS. For my own purposes, in a JavaSE environment, two helpful links as a memento for me Validation with pure Java <http://www.javaworld.com/article/2076272/java-se/validation-with-pure-java.html> Java Tip: Hibernate validation in a standalone implementation <http://www.javaworld.com/article/2137346/data-storage/java-tip-hibernate-validation-in-a-standalone-implementation.html>
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-07-22 12:09 +0200 |
| Message-ID | <87r3o0o667.fsf@Equus.decebal.nl> |
| In reply to | #32058 |
On Friday 17 Jul 2015 13:27 CEST, Jeff Higgins wrote:
> On 07/17/2015 04:36 AM, Cecil Westerhof wrote:
>> We have to remove almost every framework used in the code. I
>> started with the @NotEmpty from Hibernate. It was the only class
>> used from Hibernate, so low hanging fruit we thought.
>>
>> I wrote the following implementation:
>>
>> import java.lang.annotation.Documented;
>> import static java.lang.annotation.ElementType.FIELD;
>> import java.lang.annotation.Retention;
>> import static java.lang.annotation.RetentionPolicy.RUNTIME;
>> import java.lang.annotation.Target;
>>
>> import javax.validation.Constraint;
>> import javax.validation.constraints.NotNull;
>> import javax.validation.constraints.Size;
>> import javax.validation.ReportAsSingleViolation;
>>
>> @Documented
>> @Constraint(validatedBy = {})
>> @Target({ FIELD })
>> @Retention(RUNTIME)
>> @ReportAsSingleViolation
>> @NotNull
>> @Size(min = 1)
>> public @interface NotEmpty {
>> public abstract String message() default "Field has to be filled";
>> }
>>
>>
>> But it should be tested also of-course. So I wrote the following
>> test:
>>
>> import org.junit.Test;
>>
>> class NotEmptyClass {
>> @NotEmpty
>> String notEmpty;
>>
>> NotEmptyClass(String value) {
>> notEmpty = value;
>> }
>> }
>>
>> public class NotEmptyTest {
>> @Test
>> public void firstTest() {
>> new NotEmptyClass(null);
>> }
>> }
>>
>> I would expect this test not to pass, but it does. So clearly I am
>> doing something wrong, but I have no idea what. (This is the first
>> time I really work with annotations.) I did some Googling but until
>> now to no avail. So if someone could point me to the right
>> direction …
>>
> As I stated before I'm not a JPA or JavaEE user so this might be
> CompleteBS. I also did some googling "test custom bean validation"
> and from what I read I wonder why you don't show a
> ConstraintValidator class. And also the JUnit tests I ran across
That was the problem yes. I am complete new to this kind of stuff, but
I learned a lot now. :-D
I'll try to share my findings in the weekend, because I think that
what I have learned could be interesting for other people also.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-07-25 17:10 +0200 |
| Message-ID | <87fv4cuvb6.fsf@Equus.decebal.nl> |
| In reply to | #32104 |
On Wednesday 22 Jul 2015 12:09 CEST, Cecil Westerhof wrote:
>> As I stated before I'm not a JPA or JavaEE user so this might be
>> CompleteBS. I also did some googling "test custom bean validation"
>> and from what I read I wonder why you don't show a
>> ConstraintValidator class. And also the JUnit tests I ran across
>
> That was the problem yes. I am complete new to this kind of stuff,
> but I learned a lot now. :-D
>
> I'll try to share my findings in the weekend, because I think that
> what I have learned could be interesting for other people also.
Here it is:
https://github.com/CecilWesterhof/JavaExamples/tree/master/Annotations
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web