junit4

Mock private static final variables in the testing class

房东的猫 提交于 2019-12-01 00:40:06
I have a few private static final fields in the class I want to test. Like follows public class ClassToTest{ .... private static final Myclass myclass = MyClassFactory.getMyClass(type.firstType); .... } The type is a enum in the MyClassFactory. That factory do is it initialize object according to type passed and return. My question is does powermock support this and if so how to do this. You can use reflection also if any mock library works for you. Field f = classToTest.getclass().getDeclaredField("myclass "); f.setAccessible(true); f.set(classToTest,/*NEW VALUE*/); DaveH PowerMock ( + a

How to continue test after JUnit ExpectedException if thrown?

末鹿安然 提交于 2019-11-30 22:08:28
问题 I have set up some JUnit (4.12) test with the ExpectedException feature, and I would like the test to continue after the expected exception. But I never see the log '3', as the execution seems to stop after the exception, event if catch? Is this actually possible, and how? @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testUserAlreadyExists() throws Exception { log.info("1"); // Create some users userService.createUser("toto1"); userService.createUser(

How can I get @BeforeClass and @AfterClass equivalent in Junit3?

冷暖自知 提交于 2019-11-30 18:29:26
I want to back up my application's database before replacing it with the test fixture. I'm forced to use Junit3 because of Android limitations, and I want to implement the equivalent behavior of @BeforeClass an @AfterClass. UPDATE: There is now a tool ( Junit4Android ) to get support for Junit4 on Android. It's a bit of a kludge but should work. To achieve the @BeforeClass equivalent, I had been using a static variable and initializing it during the first run like this, but I need to be able to restore the database after running all the tests. I can't think of a way of detecting when the last

How to click in a specific part of a Button using Selenium, for the list of options to be displayed?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:15:46
问题 <table id="ext-comp-1389" class="x-btn x-btn-text-icon " cellspacing="0" style="width: auto;"> <tbody class="x-btn-small x-btn-icon-small-left"> <tr> <tr> <td class="x-btn-ml"> <td class="x-btn-mc"> <em class="x-btn-split" unselectable="on"> <button id="ext-gen128" class="x-btn-text create" type="button">New</button> </em> </td> <td class="x-btn-mr"> <i> </i> </td> </tr> <tr> </tbody> </table> Above is how the HTML element is embedded..! The HTML element is a 'New' button with a '+' sign next

Automatic mock instantiation in a Spring JUnit test

纵饮孤独 提交于 2019-11-30 17:52:40
I have a Spring XML bean definition that I want to write integration tests for. The XML bean definition is part of a larger application context where several such files are included using <import> . Inside the definition, I reference several beans that are coming from other files. For my integration test I would like to instantiate the definition standalone and use Mockito mocks for all other beans. Until now, I am using something like this: FooIntegrationTest.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class FooIntegrationTest { @Autowired private ClassUnderTest

Cannot resolve symbol InstantTaskExecutorRule

纵饮孤独 提交于 2019-11-30 17:25:20
I open example code BasicRxJavaSample (from this article Room+RxJava ) The main thing is there: @Rule public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule(); And BasicRxJavaSample is all ok. But I can not apply this in my test. That's what's going on: Cannot resolve symbol InstantTaskExecutorRule And manual import does not work: My autocompletion works like this But should be so My app build.gradle ( full gradle here ): // tests testImplementation 'junit:junit:4.12' androidTestCompile "com.android.support:support-annotations:$supportVersion" testImplementation

How to make JUnit test cases to run in sequential order?

ⅰ亾dé卋堺 提交于 2019-11-30 17:12:19
I am using JUnit4. I have a set of test methods in a test case. Each test method inserts some records and verify a test result and finally delete the records inserted. Since the JUnit run in parallel, test methods fail because of some records present during the execution of previous test method. This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6). What we need is that the test methods have to pass in all our machines. I have tried clearing the records in the Setup() method but again it works only on my machine. Is there any option available in JUnit to make the

How to make JUnit test cases to run in sequential order?

空扰寡人 提交于 2019-11-30 16:29:10
问题 I am using JUnit4. I have a set of test methods in a test case. Each test method inserts some records and verify a test result and finally delete the records inserted. Since the JUnit run in parallel, test methods fail because of some records present during the execution of previous test method. This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6). What we need is that the test methods have to pass in all our machines. I have tried clearing the records in the

How to mock local variables using mockito or powermock

前提是你 提交于 2019-11-30 15:51:22
I have scenario like this InputStreamReader reader = new InputStreamReader(getFileAsStream(resourceResolver, iconpath)); BufferedReader bReader = new BufferedReader(reader); I have mocked till this point getFileAsStream(resourceResolver, iconpath) now I am getting one reader BufferedReader bReader = new BufferedReader(reader); but when I execute this line I get null and not able to move forward while ((iconEntry = bReader.readLine()) != null) Please tell me how can I mock this. Please note I cannot change my main code therefore the solution present on Mockito docs is not valid in my case Test

Exclude specific tests from being run in parallel in jUnit

狂风中的少年 提交于 2019-11-30 13:58:00
问题 I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project's pom.xml file: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <parallel>classes</parallel> </configuration> </plugin> I've discovered that there are 2 test-classes (let's call them "badtestclass1" and "badtestclass2") that keep getting penalized by this parallel execution due to the way the tests