Tag: unit testing

Java自定义Annotation聚合多个注释

我正在为我的RestControllers编写一个TestCases 对于每个ControllerTest calss我使用以下注释 @WebAppConfiguration @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class}) 所以,我决定定义我自己的注释,包含所有这些注释 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @WebAppConfiguration @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class}) public @interface ControllerTest { } 然后,我只为所有的ControllerTest classes使用了一个注释 @ControllerTest public class XXControllerTest { } 在此修改后,测试失败了 java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:115) 为了让它再次起作用,我需要将@RunWith(SpringJUnit4ClassRunner.class)添加到Test class @ControllerTest @RunWith(SpringJUnit4ClassRunner.class) public class XXControllerTest { } 我的问题是为什么我的@ControllerTest注释在包含@RunWith(SpringJUnit4ClassRunner.class)注释时@RunWith(SpringJUnit4ClassRunner.class) ? […]

获取EasyMock模拟对象以抛出exception

我正在使用EasyMock为许多协作类编写unit testing。 其中一个类(称为Foo )打开与远程服务器的网络连接,并将该服务器的XML响应解析为其余类可以使用的内容。 目前,我的测试仅包括一切都很糟糕且远程服务器启动并运行并按预期返回XML的场景。 但是,如果我可以模拟Foo以便模拟在远程服务器关闭时会发生什么,或者存在导致Foo抛出IOException其他问题,我会更高兴。 我看过EasyMock API,我看不到任何看起来像是要求模拟抛出exception的方法。 对于我来说,进行基于Exception的测试对我来说并不是绝对必要的,但我很好奇是否可以使用EasyMock,而且我认为以这种方式测试Foo的公共合同会很有用。 以前有人用EasyMock做过这样的事吗? 参考 EasyMock API

确保对新Subversion提交的覆盖率最小

我们有一个大型项目,几乎没有任何unit testing。 我想从现在开始确保开发人员提交新function(或错误!),而没有相应unit testing的最小覆盖范围。 有哪些方法可以强制执行此操作? 我们使用很多工具,所以也许我可以使用插件(jira,greenhopper,fisheye,sonar,hudson)。 我也在考虑一个Subversion预提交钩子,jira的Commit Acceptance插件,或类似的东西。 思考?

使用Powermock时NoClassDefFoundError

我正在使用PowerMock测试运行器运行junit测试用例。 我正在使用以下命令行来执行它: java -cp .:junit-4.9b2.jar:easymock-3.0.jar:powermock-easymock-1.4.8-full.jar org.junit.runner.JUnitCore SampleTest 这样做时,我收到此错误: initializationError(SampleTest) java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner … 我该如何解决?

如何在另一个测试类中重用现有的JUnit测试?

如何在另一个测试类中重用JUnit测试? 例如: public TestClass1 { @Test public void testSomething(){…} } public TestClass2 { @Test public void testSomethingAndSomethingElse() { // Somehow execute testSomething() // and then test something else } }

如何用Java启动和停止Tomcat容器?

我有一个Maven项目,它启动一个tomcat容器进行预集成测试(jUnit Tests)。 我的大多数测试都要求重新启动测试中的Web应用程序。 所以我想在执行每个jUnit测试之前重启Tomcat容器。 至于现在我使用cargo-maven2-plugin来配置tomcat容器。 那么,是否可以使用java语句启动和停止容器?

我应该如何在Java中测试私有方法?

可能重复: unit testing私有方法的最佳方法是什么? 我是一名初学程序员,我不知道如何编写一个结构良好的unit testing应用程序。 我想编写能够在之后添加有效unit testing的应用程序。 问题在于private方法 – 它们不能在类之外进行测试。 我是否应该通过更改所有protected private方法来解决此问题,并让测试类扩展源类? 或者有更好的解决方案吗? 我的解决方案(private splitLetters => protected splitLetters)将如下工作: 来源类: class MyClass{ protected splitLetters(int num){ return num+2; } } 测试类: class Test_MyClass extend MyClass{ public splitLettersTest(){ for(int i=0;i<100;i++){ System.println(parent.splitLetters(i)); } } } 解决方案: 不测试私有方法 – 有时私有方法正在执行非常复杂的任务,应该进行非常好的测试,我们不希望该用户可以访问这些方法。 很快,解决方案正在将私有方法更改为受保护。 嵌套类测试方法 – 有问题,因为QA在源代码中进行了更改 反思 – 如果这可以调用私有方法,它看起来像一个很好的解决方案http://www.artima.com/suiterunner/private3.html (我应该学习更多来理解reflection。我不明白reflection是怎么做的如果我们可以从另一个类调用私有方法,那就不要破坏拥有公共和私有方法的所有想法。) 没有定义私有方法 (正如我在我的解决方案中所示) […]

如何使用Powermock模拟void静态方法抛出exception?

我试图使用Powermock和Mockito来模拟一个void静态方法来抛出exception,如下所示。 但我遇到了一个问题。 除非我使用相同的参数对Adder.add()进行两次调用,否则不会抛出模拟的IOException 。 顺便说@RunWith(PowerMockRunner.class) ,我已经将@RunWith(PowerMockRunner.class)和@PrepareForTest(Adder.class)到unit testing类中。 class Adder{ public static void add(int i) throws IOException{ return; } } @Test public void testAdder() throws IOException{ PowerMockito.mockStatic(Adder.class); PowerMockito.doThrow(new IOException()).when(Adder.class); Adder.add(12); try { Adder.add(11); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // assert things } 提前致谢。 🙂 答案如下。 在咨询http://code.google.com/p/powermock/issues/detail?id=278之后 ,实际上上面的Adder.add(12)是设置模拟静态方法的一部分。 这意味着在使用参数12调用Adder.add()时,将抛出IOException。 这很难理解,对吧? […]

用mockito嘲笑一个单身人士

我需要测试一些遗留代码,它在方法调用中使用单例。 测试的目的是确保clas sunder测试调用单例方法。 我在SO上看到过类似的问题,但所有的答案都需要其他依赖项(不同的测试框架) – 我不幸的是仅限于使用Mockito和JUnit,但这对于这样的流行框架来说应该是完全可能的。 单身人士: public class FormatterService { private static FormatterService INSTANCE; private FormatterService() { } public static FormatterService getInstance() { if (INSTANCE == null) { INSTANCE = new FormatterService(); } return INSTANCE; } public String formatTachoIcon() { return “URL”; } } 被测试的课程: public class DriverSnapshotHandler { public String getImageURL() { return […]

Selenium页面对象重用

我真的很喜欢selenium 2按照惯例推动你使用PageObjects作为POJO,然后简单地使用PageFactory来实例化这个类中的字段。 我发现限制的是我们在许多不同的页面上重用了很多元素。 最大的问题是这些重用的组件在出现在不同页面时没有相同的id / name; 但是我们为每个测试运行的测试是相同的。 例如,我们在很多地方收集日期。 因此,此示例页面对象可能是(月,日字段已删除): public class DatePageObject { private WebDriver driver; DatePageObject(WebDriver driver) { this.driver = driver; } @FindBy( id = “someIdForThisInstance”) private WebElement year; public void testYearNumeric() { this.year.sendKeys(‘aa’); this.year.submit(); //Logic to determine Error message shows up } } 然后我可以使用下面的代码简单地测试一下: public class Test { public static void main(String[] args) […]