Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to test my @NotEmpty with JUnit

1,004 views
Skip to first unread message

Cecil Westerhof

unread,
Jul 17, 2015, 4:44:12 AM7/17/15
to
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

Joerg Meier

unread,
Jul 17, 2015, 6:29:01 AM7/17/15
to
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.

Cecil Westerhof

unread,
Jul 17, 2015, 7:28:14 AM7/17/15
to
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.

Jeff Higgins

unread,
Jul 17, 2015, 7:31:56 AM7/17/15
to
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...

Jeff Higgins

unread,
Jul 17, 2015, 8:02:13 AM7/17/15
to
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>

Jeff Higgins

unread,
Jul 17, 2015, 9:18:11 AM7/17/15
to
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/>

Jeff Higgins

unread,
Jul 17, 2015, 11:03:40 AM7/17/15
to
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>


Jeff Higgins

unread,
Jul 17, 2015, 11:43:55 AM7/17/15
to
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>


Cecil Westerhof

unread,
Jul 22, 2015, 5:59:52 AM7/22/15
to
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

unread,
Jul 22, 2015, 6:14:47 AM7/22/15
to
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

unread,
Jul 25, 2015, 11:16:03 AM7/25/15
to
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
0 new messages