如何获得“真实”的体验?使用 EJBContainer 时,jUnit 中出现异常而不是 EJBException?

发布于 2024-12-22 23:12:45 字数 1805 浏览 2 评论 0原文

我正在我的 Java EE 应用程序中设置单元测试。我正在使用 JPA、JSF、Netbeans 和 Glassfish。这也是我的第一个真正的java应用程序,所以如果答案是愚蠢的,请原谅我!

该测试使用 EJBContainer,访问实体并尝试输入空记录。然后它尝试输入用户名太短的记录。 我想确认是否抛出了正确的异常。

我可以添加@Test(expected=javax.ejb.EJBException.class)但这将捕获容器可能出现的任何异常扔。如果这不是预期的异常,我想知道它。 (与捕获通用异常的原理相同,最佳实践是捕获特定异常)

以下是帮助说明的测试:

//@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
}

以下是代理实体的“用户名”属性的注释:

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

如您所见,两个测试都应该抛出 javax.validation.ConstraintViolationException 或其他一些异常。我可以在 EJBException 内的调试信息中看到它们。我不确定是否有办法“提取”正确的异常?

最后,我是个十足的菜鸟。因此,如果我走错了路,请告诉我。

谢谢。

--更新--

作为对斯蒂芬的回应,这是我得出的结果。

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.");
        }
    }

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.");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-12-29 23:12:45

您必须在单元测试中显式捕获EJBException,调用getCause()来提取真正的异常,然后根据需要进行测试。

(这是测试在版本 3 JUnit 测试中抛出正确异常的方法。)

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.)

蒗幽 2024-12-29 23:12:45

您可以对预期的异常使用规则。是这样的:

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testCreate()  {
     thrown.expect(<insert here conditions about the exception thrown>);
     //...
}

我不确定这一点,但我认为它适用于 JUnit 4.7

You can use rules for the expected exception. It's something like this:

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testCreate()  {
     thrown.expect(<insert here conditions about the exception thrown>);
     //...
}

I'm not sure about this, but I think it's for JUnit 4.7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文