junit

how to use mockito spy to redirect a method call?

左心房为你撑大大i 提交于 2019-12-24 17:17:27
问题 I have a method that relies on "now" Date object. I want to write a unit-test for it. So I want to inject a fake-fixed "now" date (making the test determine). I have tried to inject a spy like this: private ImagesSorter setServerWithSpyImageSorter(User user, List imagesAsInsertionOrder, Date fakeNowDate) throws IOException { ImagesSorter imagesSorter = spy(new ImagesSorter()); when(imagesSorter.sortImages(imagesAsInsertionOrder, user)).thenReturn(imagesSorter.sortImages(imagesAsInsertionOrder

Spring 4 upgrade now junit tests fails on “GenericApplicationContext has not been refreshed yet”?

点点圈 提交于 2019-12-24 16:50:53
问题 I have a spring web app that connects to some jms message brokers and am now finding test errors and failures after migrating from Spring 3.2.16 to Spring 4.2.4.RELEASE. The error below is what I am now getting after simply changing the spring version on unit tests that use JmsTemplate. org.springframework.context.support.GenericApplicationContext@12eec522 has not been refreshed yet The test classes are configured like so: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration

Play 2.0 doesn't run all tests

两盒软妹~` 提交于 2019-12-24 15:57:53
问题 I'm porting an application over to Play 2.0 and for some reason it's not running all the tests. It finds all the test classes. But just doesn't run any tests in most of them. I tried moving the classes to the base of tests directory, but that didn't make a difference. The tests it doesn't run contain tests for the persistence using hibernate JPA. The classes have a common base class for managing the database state across tests. I'm expecting the persistence tests to blow up spectacularly but

Prevent junit tests from running twice

让人想犯罪 __ 提交于 2019-12-24 15:52:25
问题 There are many similar questions to my questions,but there is no clear answer for it! My tests are failing because they are running once inside suite and once alone. And I need them to run only once inside suite. This is my suite: @RunWith(Suite.class) @Suite.SuiteClasses({Test1.class, Test2.class}) { ..... } I am running the test from the command line with command test . Has anyone found a solution for this? 回答1: I use the following setup to run tests with JUnit, parallel, and they run only

Mocking a function with pass-by-name arguments

为君一笑 提交于 2019-12-24 15:21:21
问题 My actual use-case is unit testing code involving finagle FuturePool : I want to make sure, FuturePool.apply was actually invoked, so that the task was executed in the correct instance of the pool. The problem I am running into however seems more generic, so I will illustrate it on an abstract example, not related to finagle or futures. Suppose, I have these two classes: class Foo { def apply(f: => String) = f } class Bar(val foo: Foo) { def doit(f: => String) = foo(f) } Bar has an instance

Android Tests Exception in Constructor NoClassDefFoundError

不羁的心 提交于 2019-12-24 15:12:49
问题 After solving my issues with project structure and android tests it appears that another issue is coming, when I try to run my tests i get this exception: junit.framework.AssertionFailedError: Exception in constructor: testRegisterNaming (java.lang.NoClassDefFoundError: es.unizar.vv.mobile.catmdedit.app.view.LibraryActivity at es.unizar.vv.mobile.catmdedit.app.LibraryTest.<init>(LibraryTest.java:16) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect

How to cover a method calling a static method using JUnit mocking?

自作多情 提交于 2019-12-24 15:03:52
问题 Consider two classes A and B . class A { static int a(){} } class B { void something(){ int value=A.a(); .......}} Now I have to cover class B using Junit Test case and hence I create a new class (class TestB ) to cover the class B . class TestB { @Test public void testsomething(){...} } Here my question is if there is any way I can cover the line A.a() as this is the static method. I know that I can't easy mock it because there is no object involved. So how would I proceed? I am using JUnit

Database base unit testing strategy: truncating tables between unit tests & test data

那年仲夏 提交于 2019-12-24 14:45:44
问题 I'm attempting to use JUnit to test some database code. I'm pretty new to this so please bear with me. I have a local test database which consists of four tables. Two of these tables are populated with data and the other two are empty before the program executes. The program basically performs some queries on the two tables which are populated, processes the results then writes those results to the two tables which are empty when the program is executed. There are a number of methods which

Mocking EntityManager

余生长醉 提交于 2019-12-24 14:41:55
问题 I am getting NPE while mocking EntityManager, below is my code, @Stateless public class NodeChangeDeltaQueryBean implements NodeChangeDeltaQueryLocal { @PersistenceContext private EntityManager em; @Override public String findIdByNaturalKey(final String replicationDomain, final int sourceNodeIndex, final int nodeChangeNumber) { List<String> result = NodeChangeDelta.findIdByNaturalKey(this.em, replicationDomain, sourceNodeIndex, nodeChangeNumber).getResultList(); return result.isEmpty() ? null

java.lang.NoSuchFieldError: INSTANCE exception is thrown by appium driver varriable

六眼飞鱼酱① 提交于 2019-12-24 14:23:43
问题 I am trying to write this java class that opens an apk file in an android device and presses some buttons through appium,using the code below: package new_appium_test; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium