Tag: junit

Jmockit:不能模拟net.android.Uri类的toString方法

IDE:Android Studio 2.2.2 | Jmockit:2.1 | Java:1.8.0_102-b14 我试图模拟类net.android.Uri的方法toString()。 这个类上的所有方法都可以模拟OK,但是toString()方法。 @Test public void method(@Mocked final Uri uri) throws Exception { new NonStrictExpectations() { { uri.getHost(); result = “sendViewLog”; uri.getAuthority(); result = “getAuthority”; uri.getEncodedFragment(); result = “getEncodedFragment”; uri.getPort(); result = 8080; uri.toString(); // <——- result = "helloWorld"; } }; sut.method(); } 结果:但是toString()返回null值,上面的所有方法都被模拟了。 你能给我一些解决方法来解决这个问题。 PS1:我意识到当我将鼠标hover在Expectations块内的toString方法时。 它显示此警告消息: 报告对忽略该调用结果的特定方法的任何调用。 检查使用org.jetbrains.annotations.Contract(pure […]

Spring Retry Junit:使用自定义重试策略测试重试模板

我正在尝试测试使用自定义重试策略的重试模板。 为了做到这一点,我使用这个例子: https://github.com/spring-projects/spring-retry/blob/master/src/test/java/org/springframework/retry/support/RetryTemplateTests.java#L57 基本上,我的目标是在我获得某些特定的http错误状态(例如http 500错误状态)时测试我的重试逻辑。 这是我的junit的xml上下文: CustomRetryPolicy就像: public class CustomRetryPolicy extends ExceptionClassifierRetryPolicy { private String maxAttempts; @PostConstruct public void init() { this.setExceptionClassifier(new Classifier() { @Override public RetryPolicy classify(Throwable classifiable) { Throwable exceptionCause = classifiable.getCause(); if (exceptionCause instanceof HttpStatusCodeException) { int statusCode = ((HttpStatusCodeException) classifiable.getCause()).getStatusCode().value(); return handleHttpErrorCode(statusCode); } return neverRetry(); } }); } public void setMaxAttempts(String […]

失败时删除JUnit RunListener()

我使用RunListener让它们在写入System.out时失败,但是当我失败()一个unit testing时,删除了监听器。 有没有办法让测试失败而不删除监听器? 为了澄清一个代码示例 public class OutputListenerTest { @Test public void testPrintIsDicovered() { JUnitCore runner = new JUnitCore(); // the OutputListener calls fail() when something was written runner.addListener(new OutputListener()); Result result = runner.run(TestWithOutput.class); } public static class TestWithOutput { @Test public void testOutput1() { System.out.println(“foo”); } @Test public void testOutput2() { System.out.println(“bar”); } } } […]

如何在Ant中只运行特定的JUnit测试?

我有一个样本项目结构,看起来像这样。 现在我想只通过build.xml而不是MyTest运行MyTestTwo。 我该怎么做呢? 当我尝试只运行一个测试时,我通过这样做实现了它: 如果我必须为上述项目结构做任何事情,或者如果有10个不同的测试并且我只想要运行其中的5个,我该如何实现? 我是Ant的新手,所以任何帮助都将不胜感激。 谢谢。

Mockito模拟对象从方法内的方法调用中分配一个空值

我有一个课我需要用mockito测试。 以下是课程和Mockito考试。 dbBuilder.parse(file)总是返回null,因为它调用了几个类,而这些类又调用了jar文件中的几个方法。 即使我嘲笑了所有这些,它总是返回null。 我无法跟踪空值的来源。 我试图压制这些方法,但仍无用。 由于此方法调用返回null,因此doc值为null。 因此,doc调用getElementsByTagName方法,并且运行mockito测试失败并显示NullPointerException 。 在我需要测试的此方法中,此代码后面有几行代码。 我该如何解决这个问题? class DocumentClass{ public void docMethod(){ DocumentBuilder dbBuilder = new DocumentBuilder(); Document doc = new Document(); FileStream file = new FileStream(new File(some path)); doc = dbBuilder.parse(file); NodeList nodes = doc.getElementsByTagName(“documents”); } } @RunWith(PowerMockRunner.class) @PrepareForTest({Document.class,DocumentBuilder.class,FileStream.class}) public class TestDocument{ @Test public documentTest(){ DocumentBuilder dbBuilder = PowerMockito.mock(DocumentBuilder.class); Document […]

从Eclipse内部运行JUnit测试套件

从历史上看,我总是写这样的unit testing: public void WidgetTest { @Test public void test_whatever() { // Given… // When… // Then… } } 这允许我在Eclipse和Run As >> JUnit右键单击我的测试文件,并直接从Eclipse内部测试该特定测试。 然后,在进行本地(基于Ant)构建时,我配置一个 Ant任务来立即运行我的所有src/test/java测试。 我现在正在寻找一种中间解决方案。 也就是说,只需单击一个按钮,就可以同时从Eclipse内部运行所有测试类。 一位同事建议Junit有一个“测试套件”的概念,我可以附加我的所有测试类,但看起来这个测试套件是某种我不希望包含在内的JAR /工具我的项目。 所以我问: 如何定义这样一个“测试套件”,包括我所有的测试类,并从Eclipse内部一次性地运行所有这些测试套件? 提前致谢。

Eclipse剪切/复制/粘贴不起作用

我在Eclipse Juno中安装了CodePro AnalytiX 3.7插件。 从那时起,在任何Java TestCase类中,剪切/复制/粘贴的键盘快捷键都不起作用。 当我按下键盘上的“删除”按钮时,也不会发生删除。 如果我右键单击并从菜单中选择操作,则同样可以正常工作。 在普通的Java类中,它工作正常。 请帮忙。

运行Cucumber JUnit测试时的InitializationError

package cucumber; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions( format={“pretty”}, features= “src/features/” ) public class cucumberRunner { } 使用: 黄瓜核心-1.2.4 黄瓜Java的1.2.4 黄瓜的junit-1.2.4 JUnit的-4.12 Eclipse Mars.1 java 8 错误跟踪: java.lang.NoClassDefFoundError:org / dom4j / java上java.lang.Class.privateGetDeclaredMethods(未知来源)的java.lang.Class.getDeclaredMethods0(本地方法)中的元素java.lang上的java.lang.Class.privateGetPublicMethods(未知来源)。位于cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)的cucumber.runtime.Runtime.Runtime的cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:40)中的lang.Class.getMethods(未知来源)。 (runtime.java:91)在cucumber.runtime.Runtime。(Runtime.java:69)在cucumber.runtime.Runtime。(Runtime.java:65)在cucumber.api.junit.Cucumber.createRuntime(Cucumber.java: 78)在gree.api.junit.Cucumber。(Cucumber.java:58)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (未知来源)atg.junit.internal.builders.AnnotatedBuilder.buildRunner(A中的java.lang.reflect.Constructor.newInstance(未知来源) nnotatedBuilder.java:104)org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)org.junit.internal .builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33 )org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)at org。 eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)位于org.eclipse.jdt的org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)。 org.eclipse.jdt.internal.junit.runner中的internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)。 RemoteGestRunner.run(RemoteTestRunner.java:382)位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)引起:java.lang.ClassNotFoundException:java的org.dom4j.Element。 java.lang.ClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ […]

@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 […]

我可以使用数组或哈希映射在页面对象模型中组织对象吗?

我是Selenium自动化的新手。 我对java有很好的了解。 我创建了用于用户注册的测试脚本。 我已经使用了页面对象模型。 这是我的页面对象脚本。 这是我用的 public class SIgnUpTest extends PageObject { @FindBy(id=”merchantName”) private WebElement merchant; @FindBy(id=”merchantCode”) private WebElement code; @FindBy(id=”categoryId”) private WebElement category; @FindBy(id=”description”) private WebElement description; @FindBy(id=”merchantLogo”) private WebElement logo; @FindBy(id=”btnNextStep1″) private WebElement Next; public SIgnUpTest(WebDriver driver) { super(driver); } public void enterName(String name, String code,String description){ this.merchant.sendKeys(name); this.code.sendKeys(code); this.description.sendKeys(description); } public void […]