Skip to content

Instantly share code, notes, and snippets.

@kubamarchwicki
Last active December 19, 2015 03:09
Show Gist options
  • Save kubamarchwicki/5888625 to your computer and use it in GitHub Desktop.
Save kubamarchwicki/5888625 to your computer and use it in GitHub Desktop.
Validation jbossas-remote-6 gotcha
So the answer was: I used wrong import for @Inject. Not from javax, but org.aqruillian and earlier on haven't put beans.xml in archive. With these two things sorted out - the desired-test example works fine.
-------
So the things is - I have this test to check validation on the server side.
I build arquillian archive and deploy it to a remote server: as shown
in desired-test.java.
As I'm running arquillian test suite I'd rather take this approach that
have a hibernate-validator dependency in my test scope to get validator
from default factory.
However, the test throws a NullPointerException as it's unable to inject
a Validator into a remotly deployed test. The workaround works just fine
- but I don't need Arquillian and server side deployment to run such test.
If arquillian test case is a component (like any other) why validation
resource cannot be injected? Same approach works fine for deployed
application.
The desired-test works fine with tomee-managed (so app server is run
in a separate JVM)
Injecting Validator with @Resource (with works both on TomEE and Glassfish) results with:
DEPLOYMENTS IN ERROR:
Deployment "vfs:///opt/java/jboss-6.1.0.Final/server/default/deploy/test.war" is in error due to the following reason(s): java.lang.RuntimeException: Neither any mapped-name/lookup/jndi-name specified nor any ResourceProvider could process resource-env-ref named env/pl.marchwicki.feedmanager.FeedValidationIntegrationTest/validator of type javax.validation.Validator
@Inject works fine in application (deployed not as Arquillian test)
@RunWith(Arquillian.class)
public class FeedValidationIntegrationTest {
@Inject
Validator validator;
@Deployment
public static WebArchive createDeployment() throws Exception {
return ShrinkWrap
.create(WebArchive.class, "test.war")
.addClasses(Feed.class, Item.class)
.addAsWebInfResource(EmptyAsset.INSTANCE,
ArchivePaths.create("beans.xml"));
}
@Test
public void noErrors() {
Feed f = new Feed.Builder().withTitle("test")
.withLink("http://test.com/test").build();
Item i = new Item.Builder().withTitle("Test item").build();
f.addItem(i);
Set<ConstraintViolation<Feed>> errors = validator.validate(f);
assertThat(errors.size(), equalTo(0));
}
}
@RunWith(Arquillian.class)
public class FeedValidationIntegrationTest {
@Deployment
public static WebArchive createDeployment() throws Exception {
return ShrinkWrap
.create(WebArchive.class, "test.war")
.addClasses(Feed.class, Item.class);
}
@Test
public void noErrors() {
Feed f = new Feed.Builder().withTitle("test")
.withLink("http://test.com/test").build();
Item i = new Item.Builder().withTitle("Test item").build();
f.addItem(i);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Feed>> errors = validator.validate(f);
assertThat(errors.size(), equalTo(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment