我有一个抽象类:
@Component
public abstract class BaseReport {
public void export() {
...
}
Run Code Online (Sandbox Code Playgroud)
还有一堆扩展它的类,并覆盖export()方法(或不覆盖).
@Component
public final class Report1 extends BaseReport
@Component
public final class Report2 extends BaseReport
Run Code Online (Sandbox Code Playgroud)
我的大多数测试都会自动装配扩展BaseReport的具体类,没有任何问题:
public class Report1Test extends BaseTest {
@Autowired
Report1 _report;
public class Report2Test extends BaseTest {
@Autowired
Report2 _report;
Run Code Online (Sandbox Code Playgroud)
这适用于扩展 BaseReport 的所有类的自动装配.但是我还需要自动抽象抽象类本身BaseReport来测试export()方法.
public class BaseReportTest extends BaseTest {
@Autowired
BaseReport _report;
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到了臭名昭着的:
没有定义BaseReport类型的唯一bean:期望的单个匹配bean但找到2 [Report1,Report2].
我已经尝试过使用@Qualifier,但@Qualifier的问题是(据我所知)你用它来告诉Spring哪个类 - 实现一个接口或扩展一个Abstract类 - 你想使用它.但那不是我的情况.我想使用抽象类本身.
我也试过使用@Resource,像这样:
public class BaseReportTest extends BaseTest {
@Resource(name = "baseReport")
BaseReport _report; …Run Code Online (Sandbox Code Playgroud) 这非常简单,与Struts 2.1.x完美配合.但我们最近升级到2.3.15.2并且它破了.基本上我们有一个表单(实际上,很多表单)有多个提交:
<s:form>
<s:submit action="save" />
<s:submit action="resetPassword" />
</s:form>
Run Code Online (Sandbox Code Playgroud)
如果我坚持标签中的动作一切都很好.但如果它在标签中,我会收到404错误.这是同样的行动!
我一直在调试并发现当你在标签中使用"action"属性时,生成的html是:
<input type="submit" name="action:save">
<input type="submit" name="action:resetPassword">
Run Code Online (Sandbox Code Playgroud)
据说Struts应该采用这个"动作"前缀并说"A-ha!这是一个动作!" 并执行它.它或多或少都是这样.或至少尝试.我发现的是,在非常低的级别,DefaultActionMapper.handleSpecialParameters()方法遍历所有参数并尝试为每个参数创建一个ParameterAction,如果它不为null,则执行它.大多数参数产生"null"ParameterAction,但不产生"action:".
在文档中我发现了这个关于ParameterAction的内容:
Defines a parameter action prefix. This is executed when the configured prefix key is
matched in a parameter name, allowing the implementation to manipulate the action mapping
accordingly. For example, if the "action:foo" parameter name was found, and a
ParameterAction implementation was registered to handle the "action" prefix, the execute
method would be called, allowing the implementation to set …Run Code Online (Sandbox Code Playgroud) 我试图只删除我使用@DatabaseSetup注释插入的记录.
我的测试看起来像这样:
@Test
@DatabaseSetup("classpath:data-set.xml")
@DatabaseTearDown(value={"classpath:data-set.xml"}, type= DatabaseOperation.DELETE)
public void testSomething() throws Exception {
Run Code Online (Sandbox Code Playgroud)
数据set.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<person id="1" name="Joe"/>
</dataset>
Run Code Online (Sandbox Code Playgroud)
据推测,使用DatabaseOperation.DELETE意味着"删除与数据集中的行匹配的数据库表行".但是当我执行测试时,它会擦除表格中的所有数据.
我认为问题是用于拆卸的xml文件的格式.我尝试使用不同的格式:
首先尝试使用tear-down.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<person id="1"/>
</dataset>
Run Code Online (Sandbox Code Playgroud)
第二次尝试使用tear-down.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<person/>
</dataset>
Run Code Online (Sandbox Code Playgroud)
无论使用何种格式,它总是删除表中的所有数据.我找不到用于拆除的格式的单个例子,而不是简单地列出表名.在大多数示例中,人们似乎都使用相同的xml文件进行设置和拆卸.
但这必须是可能的,不是吗?