junit4

Difference between stub and when in mockito

陌路散爱 提交于 2019-12-02 23:26:19
I am new to mockito. need to know difference between stub and when 1. stub(cpproxy.getBinList()).toReturn(gettestbins()); 2. when(cpproxy.getBinList()).thenReturn(gettestbins()); whats the difference between these two? Brice Actually they are technically the same. When Mockito was first created, we were talking about stubs, so the vocabulary followed that idea. Later people thought it was better to think in interactions rather that technical terms, so the vocabulary followed the when ... then ... style. This change in vocabulary helps people to think about interactions , messaging between

PowerMock throws NoSuchMethodError (setMockName)

北城余情 提交于 2019-12-02 22:55:29
I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings; at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock

How to use VisibleForTesting for pure JUnit tests

折月煮酒 提交于 2019-12-02 22:32:38
I´m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTesting clearly without making the thing manually public. Ex: @VisibleForTesting public Address getAddress() { return mAddress; } The method has to be public to let it be "public" to tests, but in that case the annotation doesn't make sense right? why not just use a comment if the annotation will not do nothing? According to Android docs: You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within

JUnit assertions : make the assertion between floats

馋奶兔 提交于 2019-12-02 21:45:35
I need to compare two values : one a string and the other is float so I convert the string to float then try to call assertEquals(val1,val2) but this is not authorized , I guess that the assertEquals doesn't accept float as arguments. What is the solution for me in this case ? You have to provide a delta to the assertion for Floats: Assert.assertEquals(expected, actual, delta) While delta is the maximum difference (delta) between expected and actual for which both numbers are still considered equal. Assert.assertEquals(0.0012f, 0.0014f, 0.0002); // true Assert.assertEquals(0.0012f, 0.0014f, 0

How do I programmatically run all the JUnit tests in my Java application?

▼魔方 西西 提交于 2019-12-02 20:57:55
From Eclipse I can easily run all the JUnit tests in my application. I would like to be able to run the tests on target systems from the application jar, without Eclipse (or Ant or Maven or any other development tool). I can see how to run a specific test or suite from the command line. I could manually create a suite listing all the tests in my application, but that seems error prone - I'm sure at some point I'll create a test and forget to add it to the suite. The Eclipse JUnit plugin has a wizard to create a test suite, but for some reason it doesn't "see" my test classes. It may be looking

Selenium 2 and JUnit4: How to capture screenshot on exception?

你离开我真会死。 提交于 2019-12-02 20:42:04
I want to capture a screenshot only upon unexpected exception. Alberto Note.- This answer could be outdated. The answer is based on Selenium 2.15 Using TestWatcher does the trick (the unit test must extend following BaseTest ): public abstract class BaseTest { // ... protected WebDriver driver; @Rule public TestRule testWatcher = new TestWatcher() { @Override public void starting(Description desc) { LOG.info("Launching browser..."); driver = Utils.getFirefoxDriver(); } @Override public void finished(Description desc) { LOG.info("Quitting driver..."); driver.quit(); } @Override public void

Run all tests in Junit 4

谁说我不能喝 提交于 2019-12-02 18:53:08
I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them? I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase The test classes are JUnit 4, so their

Inject mock into Spring MockMvc WebApplicationContext

十年热恋 提交于 2019-12-02 18:51:13
I'm working to test (via JUnit4 and Spring MockMvc) a REST service adapter using Spring-boot. The adapter simply passes along requests made to it, to another REST service (using a custom RestTemplate ) and appends additional data to the responses. I'd like to run MockMvc tests to perform controller integration tests, but want to override the RestTemplate in the controller with a mock to allow me to predefine the 3rd party REST response and prevent it from being hit during each test. I've been able to accomplish this by instantiating a MockMvcBuilders.standAloneSetup() and passing it the

IntelliJ IDEA cannot see Lombok generated code

大城市里の小女人 提交于 2019-12-02 18:44:10
I have a Gradle-based project that uses lombok. I have imported this project into IntelliJ IDEA 14.1 (using the Import External Model import method). I can run the JUnit4 unit tests without problem in Gradle, but IntelliJ seems to have a problem seeing the Lombok generated Getters. This is preventing me from running the tests in IDEA. To make sure it wasn't a set up issue, I created a Very simple project and confirmed that the same issue occurs in the simple test project. My versions: Gradle: gradle-2.3-all via Gradle wrapper Intellij IDEA: IU-141.713 Lombok Plugin: 0.9.2 What am I missing

Unit test Java class that loads native library

淺唱寂寞╮ 提交于 2019-12-02 18:42:19
I'm running unit tests in Android Studio. I have a Java class that loads a native library with the following code static { System.loadLibrary("mylibrary"); } But when I test this class inside my src/test directory I get java.lang.UnsatisfiedLinkError: no mylibrary in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) How can I make it find the path of native .so libraries which is located at src/main/libs in order to unit test without errors? Note: inside src/main