Java 有没有一种方法可以创建一个spring上下文,它可以在不同类上的所有junit单元测试之前运行?
对不起,我没有在互联网上深入研究这个问题。因此,反对票是受欢迎的。毕竟我有一种感觉,这可能是一个垃圾问题。然而,我给了一个机会Java 有没有一种方法可以创建一个spring上下文,它可以在不同类上的所有junit单元测试之前运行?,java,spring,junit,jndi,Java,Spring,Junit,Jndi,对不起,我没有在互联网上深入研究这个问题。因此,反对票是受欢迎的。毕竟我有一种感觉,这可能是一个垃圾问题。然而,我给了一个机会 我有一个包含所有单元测试的项目。所有单元测试都需要一个基本上创建jndi名称空间的设置。我在每个单元测试类上使用@Before标记来完成这项工作。所以我做了大量的复制粘贴,这让我有点恼火。有没有一种方法可以创建一个singleton类来为我的所有单元测试创建这个jndi名称空间,可能是使用spring或其他任何方法。您可以在一个JNDitesExecutionListe
我有一个包含所有单元测试的项目。所有单元测试都需要一个基本上创建jndi名称空间的设置。我在每个单元测试类上使用@Before标记来完成这项工作。所以我做了大量的复制粘贴,这让我有点恼火。有没有一种方法可以创建一个singleton类来为我的所有单元测试创建这个jndi名称空间,可能是使用spring或其他任何方法。您可以在一个JNDitesExecutionListener类中实现jndi逻辑,该类实现了 org.springframework.test.context.TestExecutionListener
另一个选项是为此创建JUnit规则。您可以在实现JNDitesExecutionListener的JNDitesExecutionListener类中实现JNDI逻辑 org.springframework.test.context.TestExecutionListener
另一个选项是为此创建JUnit规则。您可以在实现JNDitesExecutionListener的JNDitesExecutionListener类中实现JNDI逻辑 org.springframework.test.context.TestExecutionListener
另一个选项是为此创建JUnit规则。您可以在实现JNDitesExecutionListener的JNDitesExecutionListener类中实现JNDI逻辑 org.springframework.test.context.TestExecutionListener
另一个选项是为此创建JUnit规则。创建一个抽象类,它有一个方法来完成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注释公共静态void no arg方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass方法将在当前类的方法之前运行。创建一个抽象类,该类有一个方法来完成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注释公共静态void no arg方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass方法将在当前类的方法之前运行。创建一个抽象类,该类有一个方法来完成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注释公共静态void no arg方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass方法将在当前类的方法之前运行。创建一个抽象类,该类有一个方法来完成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注释公共静态void no arg方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass方法将在当前类的方法之前运行。感谢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");
}
}
我的单元测试使用spring上下文
测试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");
}
}
我的spring上下文如下所示:
<?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>
谢谢各位。谢谢戴纳姆先生的解决方案。如果他把答案贴出来,我会接受的 我创建了一个像这样的主类
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");
}
}
我的单元测试使用spring上下文
测试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");
}
}
我的spring上下文如下所示:
<?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>
谢谢各位。谢谢戴纳姆先生的解决方案。如果他把答案贴出来,我会接受的 我创建了一个像这样的主类
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");
}
}
我的单元测试使用spring上下文
测试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");
}
}
我的spring上下文如下所示:
<?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>
谢谢各位。谢谢戴纳姆先生的解决方案。如果他把答案贴出来,我会接受的 我创建了一个像这样的主类
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");
}
}
我的单元测试使用spring上下文
测试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");
}
}
我的spring上下文如下所示:
<?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>
谢谢大家。创建一个类,将代码放入init方法(用
@PostConstruct
注释或使用初始化bean
接口)。让我们的测试配置加载该类。。。完成。创建一个类,将代码放在init方法中(用@PostConstruct
注释或使用