我正在清理一个经过深思熟虑的包结构。这涉及到包之间的大量类移动。
eclipse 重构工具不会自动将任何相应的单元测试移动到新包中,所以我必须记得手动执行此操作。我通常会忘记这样做,因为我很懒惰、愚蠢和健忘。
我正在考虑将一些东西放在一起,以识别看起来在错误包装中的测试,但更愿意使用现成的东西。
有没有这样的工具?
当移动相应的类时, MoreUnit会自动移动测试类。
如果您的单元测试遵循标准命名约定<class>Test
,那么您可以编写一个类路径扫描器,它首先识别所有单元测试(名称以“Test”结尾的单元测试,以及@Test
至少有一次注释的单元测试),然后每个你都可以得到它的名字"com.foo.<class>Test"
,删除测试,你最终得到"com.foo.<class>"
,然后你可以检查测试本身是否有一个"com.foo.<class>"
类型的实例变量。如果没有,您可以发出警告或其他东西。
所以,假设你有这样的课
package com.foo.Bar
@NoArgsConstructor
public class Bar {
public int doSomething(){
//...
return 1337;
}
}
和一个测试:
package com.foo.fooo.BarTest
public class BarTest {
private com.foo.Bar bar;
@Before
public void setup(){
bar = new Bar();
}
@Test
public void testSomething(){
//testing and stuff
}
}
然后,您可以发现 BarTest 类是一个单元测试类(因为它有@Test
注解,并且还有一个以 Test 结尾的名称)。你可以看到实例变量栏有,并得到他们的完全限定。你会得到一个com.foo.Bar
,这是错误的,因为你的测试在 package 中com.foo.fooo
,所以你会发出一个警告,表明你的测试类与它测试的类在不同的包中。这适用于我正在编写的 90% 的测试类,当然,这是一种非常理想的情况,在实际测试中,您可能有不止一种类型的实例变量,然后您必须推断出哪一种是从单元测试的名称中测试的那个,并期望其实例变量中的至少一个是正确的类类型。
As the answer seems to be that no such tool exists I've gone ahead and put something together myself - it was natural fit for an additional feature for a tool I maintain.
In case it's of use to anyone else it's available in the 1.1-SNAPSHOT of
https://github.com/hcoles/highwheel
It's currently just a simple test that infers the name of the class under test assuming a naming convention of
Test<Foo>
or
<Foo>Test
is being followed. If that class does not exist it looks to see if another class of the same name exists in a different package.