【问题标题】:Is there a way for creating a spring context which can run before all of my junit unit tests which are on different classes? [closed]有没有办法创建一个可以在我所有不同类的junit单元测试之前运行的spring上下文? [关闭]
【发布时间】:2014-01-16 09:34:13
【问题描述】:

对不起,我没有在互联网或 SO 上深入研究这个问题。因此,欢迎投票。毕竟我有一种感觉,这可能是一个垃圾问题。不过我试了一下。

我有一个包含所有单元测试的项目。所有单元测试都需要一个基本上创建 jndi 命名空间的设置。我在每个单元测试类上都使用@Before 标记来执行此操作。所以我做了很多复制粘贴,这让我有点恼火。有没有办法创建一个单例类,为我的所有单元测试创​​建这个 jndi 命名空间,可能使用 spring 或任何其他方式。

【问题讨论】:

  • 创建一个类,将代码放入init方法中(注解@PostConstruct或使用InitializingBean接口)。让我们的测试配置加载该类...完成。

标签: java spring junit jndi


【解决方案1】:

感谢 M. Deinum 解决方案。如果他发布答案,我会接受。

我创建了一个这样的大师班

import java.io.IOException;
import java.sql.SQLException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.naming.NamingException;

public class JndiInit{

    @PostConstruct
    public void init() throws IOException, SQLException, NamingException{
        System.out.println("Mastersetup for initializing jndi namespace");
    }

    @PreDestroy
    public void tearDown(){
        System.out.println("Tear down");
    }

}

我的单元测试使用弹簧上下文 测试 1:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testContext.xml")
public class Test1{


    @Test
    public void test(){
        System.out.println("test 1");
    }

}

和测试2

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testContext.xml")
public class Test2 {

    @Test
    public void test(){
        System.out.println("test 2");
    }
}

我的春季环境是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="tr.com.mhrs.test.base.jndifactory.JndiInit">

    </bean>
</beans>

它成功实现了我想要的。

Mastersetup for initializing jndi namespace
test 1
test 2
Tear down

谢谢大家。

【讨论】:

  • 只是在这里发表评论。如果您开始创建更多测试类,则必须为它们中的每一个提供@ContextConfiguration("/testContext.xml")。为此,您可以将它拉到一个集中的父类上,这样如果您确实更改了上下文文件名,您明天就不必修改所有类(在我看来这不会发生,但仍然只是提到它:))
【解决方案2】:

你可以在一个 JNDITestExecutionListener 类中实现 JNDI 逻辑,该类实现了

org.springframework.test.context.TestExecutionListener
并使用监听器注释您的测试,如下所示
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(JNDITestExecutionListener.class)

另一种选择是为此创建 JUnit 规则。

【讨论】:

    【解决方案3】:

    创建一个抽象类,该类有一个可以进行 JNDI 设置的方法。

    编辑:再想一想,这不一定是抽象的。如果您想在所有测试类上强制执行一些设置语义,它可以是,但它不是必须的。

    用@BeforeClass 注释这个方法。

    现在,用这个父类扩展你所有的单元测试类。像这样的

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:conf/spring/test-context.xml")
    public abstract class AbstractParent{
    
    @BeforeClass
    setupJNDI(){
    ....
    }
    
    }
    
    public class MyTestClass
          extends AbstractParent{
    
    @Test
    public void myTestMethod(){
    ....
    }
    
    }
    

    关于@BeforeClass-

    有时需要多个测试共享计算成本高昂的设置(例如登录数据库)。虽然这可能会损害测试的独立性,但有时它是必要的优化。使用 @BeforeClass 注释公共静态 void 无参数方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass 方法将在当前类之前运行。

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2021-05-21
      • 2017-12-25
      • 1970-01-01
      相关资源
      最近更新 更多