junit4

How to check if a Java class contains JUnit4 tests?

六月ゝ 毕业季﹏ 提交于 2019-12-04 05:08:33
问题 I have a Java class. How can I check if the class contains methods that are JUnit4 tests? Do I have to do an iteration on all methods using reflection, or does JUnit4 supply such a check? Edit: since comments cannot contain code, I placed my code based on the answer here: private static boolean containsUnitTests(Class<?> clazz) { List<FrameworkMethod> methods= new TestClass(clazz).getAnnotatedMethods(Test.class); for (FrameworkMethod eachTestMethod : methods) { List<Throwable> errors = new

getSupportActionBar() returns null with Robolectric

こ雲淡風輕ζ 提交于 2019-12-04 05:06:18
The method getSupportActionBar() returns null when i invoke it through a Test Case based in Roboelectric and JUnit. This is my simple test case: package com.mobile.test; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import android.app.Activity; import android.content.Intent; import com.mobile.android.core.R; import com.mobile.android.core.activity.MainActivity; import

NoClassDefFoundError for MockitoInvocationHandler class

一世执手 提交于 2019-12-04 04:30:48
I am using mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar . When I run this simple unit test for mocking final method in non-final class. import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(ABC.class) public class ABCTest { @Test public void finalCouldBeMock() { final ABC abc = PowerMockito.mock(ABC.class);

junit.framework.AssertionFailedError: No tests found in xyz package

霸气de小男生 提交于 2019-12-04 04:19:00
问题 I am getting this error when I try to run Junits from ANT task. using eclipse launcher it works fine. Version of junit is 4.9 and ANT version is 1.7 Surprisingly its giving "junit.framework.AssertionFailedError" which was package structure in JUNIT 3. , in 4 it has changed to org.junit. I cross checked all libs and there is NO reference of junit 3.* version --and its working anyway using eclipse launcher. Any clue? Let me know if more detail is needed. Ant task is as follows <target name=

How to automatically migrate from JUnit 4 to JUnit 5?

心不动则不痛 提交于 2019-12-04 04:05:51
问题 In the spirit of this question from JUnit 3 to JUnit 4, are there any list of regular expressions to efficiently migrate from the junit 4 API to the junit 5 API , regardless of the code size? 回答1: The tooling at the moment is not great, but improving: IntelliJ: Migrates most annotations to JUnit 5 versions. Hovewer, does not do anything if your test file contains @Rule s (e.g., ExpectedException) as of v2018.2. Error Prone: contains built-in refactorings to automatically migrate various JUnit

Spring @ContextConfiguration

天涯浪子 提交于 2019-12-04 03:16:52
I am running the next test: import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) public class FloorServiceTest { @Autowired private FloorService floorService; @Test public void testFloorService() { floorService.findById((long)1); } }

Running Unit Tests using Maven in Spring LTW Environment

♀尐吖头ヾ 提交于 2019-12-04 03:16:09
I am developing an application in a ddd architecture using Spring LoadTimeWeaving feature. The problem is i can run my Junit tests using eclipse but not through Maven. I have tried all the options given on various sites but it simply isn't working. I get the following exception: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method.

Unit test class runs old version in eclipse

荒凉一梦 提交于 2019-12-04 03:09:55
问题 I have a JUnit test class in my project which is updated incrementally - I add tests every few weeks and sometimes modify the tests' code. Surprisingly, when I run the test class using eclipse JUnit Runner 4, it runs my old code (before the update) and not the new one. I can change the code, add or remove tests but it still runs the old code. I tried to isolate the problem and run a single test that I've just written and got the infamous "Unrooted tests" error without any stack trace to give

Mockito: when Method A.a is called then execute B.b

混江龙づ霸主 提交于 2019-12-04 02:48:35
问题 I'm using Mockito for JUnit tests. So there is a given class A that is used from the code i want to test: class A{ public A(){} public final String a(String x){ return "A.a: " + x; } } and i want to replace the Method call A.a with another method call with the same arguments and same type of return value. As you can see, its not possible to override the method a by extending the class as it is final. So what i have now is another class B with the method B.b: class B{ public B(){} public

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing in Intellij

一笑奈何 提交于 2019-12-04 02:47:10
问题 I used Intellij Idea 12 Community edition. I am trying to create test case for my class by creating test case. When i run my test case it says java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass