junit

Best practice for looped JUnit test

你说的曾经没有我的故事 提交于 2019-12-22 01:27:46
问题 In a school assignment, I should write blackbox test for a method that returns true for a parameter < 80 and false otherwise. Currently my approach would be for (int i = 0; i < 80; i++) { assertTrue(someMethod(i)); } for (int i = 80; i <= 100; i++) { assertFalse(someMethod(i)); } However, this would require 100 seperate assertions. Is there a best/better practice method? If relevant, I'm using JUnit 5 but could switch to JUnit 4 if required (it's just a school assignment after all). Regards.

Mark as failed running too long JUnit tests

风格不统一 提交于 2019-12-22 01:27:36
问题 I would like to stop and mark as failed too long running junit tests (executed within Maven 3 build). I am aware of three ways of doing it: 1) Using Test annotation with timeout parameter: @Test(timeout=100) public void testWithTimeout() { ... } 2) Using Rule annotation: @Rule public Timeout globalTimeout = new Timeout(100); 3) Configuring maven-surefire-plugin using following options: forkedProcessTimeoutInSeconds=1 reuseForks=false The clue is 1) and 2) requires to change each test (it

Why to use Mockito? [closed]

邮差的信 提交于 2019-12-22 01:02:27
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am new to Mockito and I have started to learn it. But I have some questions. Why do we need to use Mockito? As far as I know it is used to Mock(Create dummy object) and write the test cases before having actual running code. But, what if I want to test my already implemented

maven中pom.xml引入jar包小结

拟墨画扇 提交于 2019-12-22 00:43:53
用maven中的pom.xml引入包非常方便,在这我就记录一下我经常用的几个设置,配完pom.xml后只需reimport即可(不断更新中): junit: 用于测试 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> guava:google用于优化java原生类的扩展包 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> log4j: 日志相关扩展包,用system.out.print和printstacktrace在实际中是不被允许的 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.9</version> </dependency> servlet:不多说,web相关 <dependency> <groupId>javax.servlet</groupId> <artifactId

Testing和Instrumentation

≡放荡痞女 提交于 2019-12-22 00:04:20
Android 提供了一系列强大的测试工具,它针对 Android 的环境,扩展了业内标准的 JUnit 测试框架。尽管你可以使用 JUnit 测试 Android 工程,但 Android 工具允许你为应用程序的各个方面进行更为复杂的测试,包括单元层面及框架层面。 Android 测试环境的主要特征有: l 可以访问 Android 系统对象。 l Instrumentation 框架可以控制和测试应用程序。 l Android 系统常用对象的模拟版本。 l 运行单个 test 或 test suite 的工具,带或不带 Instrumentation 。 l 支持以 Eclipse 的 ADT 插件和命令行方式管理 Test 和 Test 工程。 这篇文章是对 Android 测试环境和测试方法的简要介绍,并假设你已经拥有一定的 Android 应用程序编程及 JUnit 测试的经验。 概要 Android 测试环境的核心是一个 Instrumentation 框架,在这个框架下,你的测试应用程序可以精确控制应用程序。使用 Instrumentation ,你可以在主程序启动之前,创建模拟的系统对象,如 Context ;控制应用程序的多个生命周期;发送 UI 事件给应用程序;在执行期间检查程序状态。 Instrumentation 框架

Creating a database table if it does not exist in Java production code and confirming in JUnit

拟墨画扇 提交于 2019-12-21 21:34:26
问题 I am writing a database program in Java and want to create a table if it does not already exist. I learned about DatabaseMetaData.getTables() from How can I detect a SQL table's existence in Java? and I am trying to use it: private boolean tableExists() throws SQLException { System.out.println("tableExists()"); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTables(null, null, this.getTableName(), null); System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME"));

Cucumber Java screenshots

我的梦境 提交于 2019-12-21 21:17:32
问题 Is there a way to take screenshots in between steps in Java Cucumber ? I have the following scenario : @Scenario_1 Given I log into url And I see the home page is displayed in English //Take screenshot And I click on 'Edit Profile' And I see the language set to 'English' When I change the language to Chinese //Take screenshot And I navigate to home page Then everything is displayed in Chinese //Take screenshot I want to take screenshots for certain steps of the scenario. I am currently taking

Test singleton with jUnit

我的未来我决定 提交于 2019-12-21 21:04:32
问题 I have a factory class to retrieve the configuration for my application: public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...): public Configuration getConfiguration(String

Selenium 2 WebDriver NoClassDefFoundErrorS

丶灬走出姿态 提交于 2019-12-21 20:54:20
问题 I'm working with Selenium 2 WebDriver in Eclipse and everything works fine. But when I want to test my WebDriver Testcases without Eclipse many missing classes occur. First it startet with : NoClassDefFoundError com/google/common/collect/Maps so I downloaded the guava.jar and set the classpath to it. But now the next NoClassDefFoundError occured: java.lang.NoClassDefFoundError: org/json/JSONException The errors always occur on createDriver(); I run the Selenium with JUnit in Java. "JUnit only

Invalid use of argument matchers! 0 matchers expected, 1 recorded

时间秒杀一切 提交于 2019-12-21 19:56:03
问题 Although the error is quite descriptive I could not get a hang of it. For lines: PowerMockito.when( mockStringMessageService.lookupString(Matchers.eq("XYZ"))) .thenReturn(Matchers.eq("XYZ")); Error is: [junit] Invalid use of argument matchers! [junit] 0 matchers expected, 1 recorded: [junit] -> at com.amazon.kilvish.types.StatusTableTest.setUp(StatusTableTest.java:61) [junit] [junit] This exception may occur if matchers are combined with raw values: [junit] //incorrect: [junit] someMethod