我是Spring的新手,并继承了一个Spring项目,该项目在ProjectName/WebContent/WEB-INF/applicationContext.xml中具有所有XML配置.我正在尝试将配置分解为不同的组件,以便在测试时更容易替换DataSources和Hibernate配置等内容.
这是我的文件结构:
ProjectName
->WebContent
->WEB-INF
->applicationContext.xml
->spring-datasource.xml
->spring-hibernate-properties.xml
->spring-persistence.xml
->test
->us.mn.k12... (Java pkgs with JUnit tests)
->spring-hsqldb-datasource.xml
->spring-test-bean-locations.xml
->spring-test-hibernate-properties.xml
->src
->us.mn.k12... (Java pkgs with production code)
Run Code Online (Sandbox Code Playgroud)
在WEB-INF/applicationContext.xml中,我导入以下内容:
<import resource="spring-datasource.xml"/> <!-- Production datasource -->
<import resource="spring-hibernate-properties.xml"/> <!-- Production hibernate properties -->
<import resource="spring-persistence.xml"/> <!-- DAO's, hibernate .hbm.xml mapping files -->
Run Code Online (Sandbox Code Playgroud)
该应用程序使用上述配置.
我的JUnit测试使用DbUnit和HSQLDB内存数据库运行.所以我的JUnit测试引用了spring-test-bean-locations.xml,它具有以下内容:
<import resource="spring-hsqldb-datasource.xml"/> <!-- HSQLDB datasource for test -->
<import resource="../WebContent/WEB-INF/spring-persistence.xml"/> <!-- Production DAO's, hibernate .hbm.xml mapping files -->
<import resource="spring-test-hibernate-properties.xml"/> <!-- Hibernate properties for test -->
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以指定测试数据源和hibernate属性,但是重用DAO的生成映射文件等.但是,运行JUnit测试时出错.以下是例外的相关部分:
Caused by: …Run Code Online (Sandbox Code Playgroud) 我正在学习 Python,并且在学习时使用 Pytest 来检查我的代码。这是我运行的一些示例代码:
str = "I love pizza"
str_list = list(str)
print(str_list)
print(len(str_list))
Run Code Online (Sandbox Code Playgroud)
将预期结果打印到标准输出:
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'z', 'z', 'a']
12
Run Code Online (Sandbox Code Playgroud)
但如果我运行这个测试:
def create_list_from_string():
str = "I love pizza"
str_list = list(str)
assert 123 == len(str_list)
Run Code Online (Sandbox Code Playgroud)
我无法让断言失败。我在文件中还有其他测试,这些测试按预期通过,但如果我故意编辑它们以使它们失败,则会失败。所以我认为我已经正确设置了 Pytest。我知道 Python 对代码块使用缩进,并且我验证了所有缩进都是 4 个空格,并且没有尾随制表符或空格。我也知道这assert并没有被破坏,而且我犯了某种新手错误。谢谢!