Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:41:53+00:00 2026-05-27T15:41:53+00:00

I’m setting up a unit test in my Java EE application. I’m using JPA,

  • 0

I’m setting up a unit test in my Java EE application. I’m using JPA, JSF, Netbeans and Glassfish. It’s also my first real java application so forgive me if the answer is stupid obvious!

The test uses an EJBContainer, accesses the entity and tries to enter a null record. Then it tries to enter a record with a username that is too short. I want to confirm that the proper exceptions are thrown.

I can add @Test(expected=javax.ejb.EJBException.class) but that will catch any exception the container may throw. If it’s NOT the expected exception I want to know about it. (same philosophy as catching an all purpose Exception, best practice is to catch the specific exception)

Here is the test to help illustrate:

//@Test(expected=javax.validation.ConstraintViolationException.class)
@Test(expected=javax.ejb.EJBException.class)
public void testCreate() throws Exception {
    EJBContainer container = getContainer();//pull singleton container
    AgentsFacade instance = (AgentsFacade) container.getContext().lookup("java:global/classes/AgentsFacade");

    Agents badAgent = new Agents();
    instance.create(badAgent);//null username

    //Short username
    Agents shortUsername = new Agents("srtnm");
    instance.create(shortUsername);//must be > 6 in length
}

Here are the annotations for the “username” property of the Agents entity:

...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique=true)
@Size(min=6, max=40)
@NotNull
private String username;
...

As you can see, both test should throw either javax.validation.ConstraintViolationException or some other exception. I can see them in the debugging information within the EJBException. I’m not sure if there is a way to “extract” the correct exceptions?

Finally, I’m a total noob. So if I’m trekking down the wrong road let me know.

Thanks.

–Update–

In response to Stephen, here is the result I came up with.

try {
        //Null username
        Agents badAgent = new Agents();
        instance.create(badAgent);
        fail("NULL agent added!");//should never reach this point.
    } catch (EJBException e) {
        Exception causedByException = e.getCausedByException();
        if(!(causedByException instanceof javax.validation.ConstraintViolationException)){
            fail("ConstraintViolationException wasn't thrown.");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:41:54+00:00Added an answer on May 27, 2026 at 3:41 pm

    You will have to explicitly catch the EJBException in the unit test, call getCause() to extract the real exception and then test it as required.

    (This is the way you test that the correct exceptions are thrown in a version 3 JUnit test.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.