Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: How to test my @NotEmpty with JUnit Date: Fri, 17 Jul 2015 10:36:33 +0200 Organization: Decebal Computing Lines: 58 Message-ID: <87io9jyycu.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="25785"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NiHoHkScjzVE/t60eD5YdFs/5TVvw3kU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:VN105uzr3+l48Hhc4w9ULmlfNG0= sha1:1On2VvuGg5G8qniRWiNeBDNB7lk= Xref: csiph.com comp.lang.java.programmer:32055 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