Tag: junit4

@BeforeClass和@AfterClass在每次测试之前和之后调用

我有一个非常简单的测试类,用于在Android上运行espresso测试,如下所示: import android.util.Log; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.ExternalResource; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.io.IOException; @RunWith(JUnit4.class) public class Wtf { private static class TestResources extends ExternalResource { protected void before() { println(“before() TestResources”); } protected void after() { println(“after() TestResources”); } } @ClassRule public static final TestResources […]

unit testing使用资源包的静态方法

我已经阅读了很多关于使用Powermock和Mockito的文章,并尝试了很多不同的方法,但我仍然无法找到unit testing以下静态方法的方法。 public static Map getEntries() { Map myEntriesMap = new TreeMap(); ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE); Enumeration enumList = myEntries.getKeys(); String key = null; String value = null; while (enumList.hasMoreElements()) { key = enumList.nextElement().toString(); value = myEntries.getString(key); myEntriesMap.put(key, value); } return myEntriesMap; } 代码是包含大约30个这样的静态方法的(遗留)类的一部分,并且重构实际上不是一个选项。 类似地,在一些其他静态方法中,正在检索DB连接。 例如:如何模拟资源包ENTRIES_BUNDLE并对此方法进行unit testing? 我正在寻找一种可以普遍适用于所有静态方法的模式。

Selenium Scripts在命令行上

有没有办法从命令行运行selenium webdriver测试脚本,这些脚本是通过Eclipse IDE使用Java和JUnit编写的? 我一直在尝试使用Ant或Maven,但我无法使用它。 有人可以提供一些关于去哪里的建议吗?

如何使用Whitebox模拟私有方法(org.powermock.reflect)

我想模拟一个在另一个方法中调用的私有方法。 以下是我写的示例代码。 Java代码: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch(Exception e){ // log exception } return amount; } private void updateAccount(long accountNum, long amountDeposited) throws […]

SpringJUnit4ClassRunner为每个测试初始化​​bean?

以下测试说明Spring将此测试bean初始化两次。 我希望有人可以告诉我为什么会这样,因为它应该只有一次。 这是测试: import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.InitializingBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {} ) public class TestAfterPropsSet implements InitializingBean { private static final Logger logger = Logger.getLogger(TestAfterPropsSet.class); @Test public void test1() { logger.debug(“Test1”); } @Test public void test2() { logger.debug(“Test2”); } public void afterPropertiesSet() throws Exception { logger.debug(“Bean Initialized”); […]

模拟对象创建内部方法测试中

我有一个我想测试的类。只要有可能,我会依赖于其他类的对象对该类进行dependency injection。但是,我遇到了一个案例,我想在没有重构代码的情况下模拟对象而不是申请DI。 这是被测试的课程: public class Dealer { public int show(CarListClass car){ Print print=new Print(); List list=new LinkedList(); list=car.getList(); System.out.println(“Size of car list :”+list.size()); int printedLines=car.printDelegate(print); System.out.println(“Num of lines printed”+printedLines); return num; } } 我的测试类是: public class Tester { Dealer dealer; CarListClass car=mock(CarListClass.class); List carTest; Print print=mock(Print.class); @Before public void setUp() throws Exception { dealer=new Dealer(); […]

Spring Boot集成测试不会读取属性文件

我想创建集成测试,其中Spring Boot将使用@Value注释从.properties文件中读取值。 但每次我运行测试时,我的断言都会失败,因为Spring无法读取值: org.junit.ComparisonFailure: Expected :works! Actual :${test} 我的测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebTests.ConfigurationClass.class, WebTests.ClassToTest.class}) public class WebTests { @Configuration @ActiveProfiles(“test”) static class ConfigurationClass {} @Component static class ClassToTest{ @Value(“${test}”) private String test; } @Autowired private ClassToTest config; @Test public void testTransferService() { Assert.assertEquals(config.test, “works!”); } } src / main / resource包下的application-test.properties包含: test=works! 这种行为的原因是什么?我该如何解决? 任何帮助高度赞赏。

如果测试用例失败,Selenium Web驱动程序无法关闭firefox实例

我大家,我正在使用junit与selenium web驱动程序2.28。 问题是如果我运行一个成功的测试用例,web驱动器能够关闭firefox实例,但是当测试用例失败时,selenium web驱动程序无法关闭firefox。 我正在使用FF 15.0.1与selenium-server-standalone-2.28.0.jar。 请回复谢谢Sahil private void startWebdriver() throws UIException{ //2) Prevent re-use. if(UIHandlerWD.this.profile == null) throw new UIException( UIException.Code.UI, “Webdriver instance cannot be instantiated.” ); //3) Configure Selenium Webdriver. if (this.profile.browserType.equalsIgnoreCase(“*firefox”)){ FirefoxProfile fProfile = new FirefoxProfile(); // profile.SetPreference(“network.http.phishy-userpass-length”, 255); fProfile.setAcceptUntrustedCertificates(true); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setJavascriptEnabled(true); dc.setCapability(FirefoxDriver.PROFILE, fProfile); //this.webdriver = new FirefoxDriver(dc); this.webdriver […]

如何在Junit控制台中接受来自用户的输入

我正在尝试为下面给出的函数编写Junit测试用例: class A{ int i; void set() { Scanner in=new Scanner(System.in); i=in.nextInt(); } } 现在我的问题是当我为它创建一个Junit测试用例时,除了用户的输入之外它没有: public void testSet() throws FileNotFoundException { System.out.println(“set”); A instance = new A(); int i=1; instance.set(i); // TODO review the generated test code and remove the default call to fail. //fail(“The test case is a prototype.”); } 请建议我该怎么做才能接受用户的输入。

Spring JUnit4手动/自动接线困境

我遇到了一个问题,这个问题只能解释为我对Spring的IoC容器设施和上下文设置缺乏了解,所以我会要求澄清一下。 仅供参考,我正在维护的应用程序具有以下堆栈技术: Java 1.6 spring2.5.6 RichFaces 3.3.1-GA UI Spring框架用于bean管理,Spring JDBC模块用于DAO支持 Maven用作构建管理器 JUnit 4.4现在作为测试引擎引入 我追溯(sic!)为应用程序编写JUnit测试,令我感到惊讶的是,我无法通过使用setter注入将bean注入测试类而不使用@Autowire表示法。 让我提供一个示例和附带的配置文件。 测试类TypeTest非常简单: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TypeTest { @Autowired private IType type; @Test public void testFindAllTypes() { List result; try { result = type.findAlltTypes(); assertNotNull(result); } catch (Exception e) { e.printStackTrace(); fail(“Exception caught with ” + e.getMessage()); } } } 其上下文在TestStackOverflowExample-context.xml定义: […]