Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32104

Re: How to test my @NotEmpty with JUnit

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: How to test my @NotEmpty with JUnit
Date 2015-07-22 12:09 +0200
Organization Decebal Computing
Message-ID <87r3o0o667.fsf@Equus.decebal.nl> (permalink)
References <87io9jyycu.fsf@Equus.decebal.nl> <moap0a$qil$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web