Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 春季及;JUnit:使用xml而不是注释进行配置_Java_Xml_Spring_Junit - Fatal编程技术网

Java 春季及;JUnit:使用xml而不是注释进行配置

Java 春季及;JUnit:使用xml而不是注释进行配置,java,xml,spring,junit,Java,Xml,Spring,Junit,因此,对于Spring,我更喜欢xml而不是注释。这只是个人喜好,我喜欢让xml文档统一我的spring配置数据 无论如何,我正在为数据库访问编写一个JUnit测试用例。我第一次使用spring测试库。我正在尝试使用依赖项注入将StudentDAOBean注入到下面的类中: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration ({"classpath:/test-context.xml"}) public class Stu

因此,对于Spring,我更喜欢xml而不是注释。这只是个人喜好,我喜欢让xml文档统一我的spring配置数据

无论如何,我正在为数据库访问编写一个JUnit测试用例。我第一次使用spring测试库。我正在尝试使用依赖项注入将StudentDAOBean注入到下面的类中:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration ({"classpath:/test-context.xml"})
public class StudentDaoTest extends TestCase {

private StudentDao studentDao;

public StudentDao getStudentDao() {
    return studentDao;
}

public void setStudentDao(StudentDao studentDao) {
    this.studentDao = studentDao;
}

@Test
@Transactional
public void test(){
    //This ends up printing null, identifying the problem
    if(studentDao == null){
        System.out.println("Null");
    }

    Student student = new Student();
    student.setFirstName("First");
    studentDao.insertStudent(student);
}
}

问题是,正如你从评论中猜到的,这不起作用。test-context.xml文件启动,它导入的另一个上下文文件也启动,正如我从日志中看到的,因此程序并不是找不到该文件。不知何故,我拥有的xml文档只是没有正确配置bean

<?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"> 

<import resource="data-context.xml"/>

<bean id="studentDaoTest" class="io.craigmiller160.schedule.persist.StudentDaoTest">
    <property name="studentDao" ref="studentDao"/>
</bean>

我发现如果我在studentDao上使用@Autowired注释,它确实可以工作。问题是,我不在程序中的任何其他地方使用注释,我希望保持一致性。老实说,我也希望避免使用@ContextConfiguration,但我认为我无法做到这一点

因此,我正在寻求帮助,使这种注入只使用xml。我知道,我很挑剔,但正如我所说,我喜欢我的一贯性

提前谢谢

PS.文件的完整文件路径为:

StudentDaoTest:src/test/java/io/myname/schedule/persist/StudentDaoTest
test-context.xml:src/test/resources/test-context.xml

注入应用程序上下文,并根据需要使用它:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration ({"classpath:/test-context.xml"})
public class StudentDaoTest extends TestCase {
    @Autowired
    private ApplicationContext ctx;

实际上,StudentDaoTestbean的定义对测试没有影响,因为测试框架中没有这样的函数。

如果不想使用Spring注释,为什么要使用它们

public class MyTest {

  private ConfigurableApplicationContext context;
  @Before
  public void initApplicationContext() {
    context = new ClassPathXmlApplicationContext("...");
  }
  @After
  public void closeApplicationContext() {
    if (context != null) {
      context.close();
      context = null;
    }
  }

  @Test
  public void test() {
    context.getBean(Object.class);
    // ...
  }
}

这会让我使用@Transactional JTA注释吗?这是我唯一想要的。如果不使用此测试类中的其他注释,我无法使其工作。
Transactional TestExecutionListener不提供任何
@Transactional
支持。只是想确定一下。唯一的限制是关于项目中Spring配置的一致性?如果是这样,请继续使用
@contextconfiguration on
(这是
弹簧测试的一部分)
)和良好的旧配置