junit4

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

ぃ、小莉子 提交于 2019-11-30 13:41:33
问题 Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") { @Override protected String featureValueOf(IndexableField actual) { return actual.name(); } } )); Commented lines 152–157 in IndexerTest.java (commit ac72ce) Causes a NoSuchMethodError (see http://db.tt/qkkkTE78 for complete output): java.lang

Mock class in class under test

Deadly 提交于 2019-11-30 13:15:42
问题 How I can mock with Mockito other classes in my class which is under test? For example: MyClass.java class MyClass { public boolean performAnything() { AnythingPerformerClass clazz = new AnythingPerformerClass(); return clazz.doSomething(); } } AnythingPerformerClass.java class AnythingPerformerClass { public boolean doSomething() { //very very complex logic return result; } } And test: @Test public void testPerformAnything() throws Exception { MyClass clazz = new MyClass(); Assert.assertTrue

How to programmatically execute a test suite using JUnit4?

假装没事ソ 提交于 2019-11-30 12:51:52
I am trying to invoke a JUnit Test suite using the API. I know that you can suite up test classes using the following: @RunWith(Suite.class) @Suite.SuiteClasses({ Test1.class, Test2.class, ... }) But, is there a way to trigger the entire suite using the Java API, using JUnitCore for example? For example, you can trigger a test by using the following code: Runner r = try { r = new BlockJUnit4ClassRunner(Class.forName(testClass)); } catch (ClassNotFoundException | InitializationError e) { // handle } JUnitCore c = new JUnitCore(); c.run(Request.runner(r)); Update: From the API, it seems that the

Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'?

心不动则不痛 提交于 2019-11-30 12:38:08
问题 I am migrating my app to androidx, I can't seem to get my unit tests working. I took example from Google's AndroidJunitRunnerSample, which has been updated to use the new androidx api. I get the following error when trying to run my tests: java.lang.Exception: Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded. Check your build configuration. Here is my module build.gradle: android { defaultConfig { testInstrumentationRunner

Testing for custom plugin portlet: BeanLocatorException and Transaction roll-back for services testing

寵の児 提交于 2019-11-30 12:30:51
问题 My Problems: I can test successfully for CRUD services operation. I was doing an insert on @Before [setUp()] and delete of same data on @After [tearDown()] but going forward I would need to support Transactions rather than writing code for insert and delete. I am successful in fetching single records of my entity but when I fire a search query or try to fetch more than one of my entities I get: com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set for servlet

JUnit4 run all tests in a specific package using a testsuite

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 11:02:32
Is this possible in JUnit4? In JUnit3, I would do the following: public class MyTestSuite { public static Test suite() throws Exception { doBeforeActions(); try { TestSuite testSuite = new TestSuite(); for(Class clazz : getAllClassesInPackage("com.mypackage")){ testSuite.addTestSuite(clazz); } return testSuite; } finally { doAfterActions } } ... } The takari-cpsuite (originally developed by Johannes Link ) offers a classpath-suite which should fit your needs. It allows filtering of classes in the Classpath by regular expressions like: import org.junit.extensions.cpsuite.ClasspathSuite.*; ...

Need help to write a unit test using Mockito and JUnit4

不羁岁月 提交于 2019-11-30 11:01:42
Need help to write a unit test for the below code using Mockito and JUnit4, public class MyFragmentPresenterImpl { public Boolean isValid(String value) { return !(TextUtils.isEmpty(value)); } } I tried below method: MyFragmentPresenter mMyFragmentPresenter @Before public void setup(){ mMyFragmentPresenter=new MyFragmentPresenterImpl(); } @Test public void testEmptyValue() throws Exception { String value=null; assertFalse(mMyFragmentPresenter.isValid(value)); } but it returns following exception, java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked. See http://g.co

Exclude specific tests from being run in parallel in jUnit

余生长醉 提交于 2019-11-30 08:57:12
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 in them have been written. Ideally, I would refactor those test-classes to behave better, but in the

Create my own MethodSorter for junit's @FixMethodOrder

守給你的承諾、 提交于 2019-11-30 08:37:18
问题 junit 4.11 comes with the @FixMethodOrder -annotation, which makes it possible to select a MethodSorter-implementation to order the junit tests. There are three default sorters, JVM , NAME_ASCENDING and DEFAULT . Now, I would like to create my own MethodSorter. Can anyone help me with any pointers to how to do that? 回答1: Short Answer This isn't easy, and is discouraged, because JUnit doesn't encourage dependent tests. Long Answer For more information, see extended discussion on

Mock class in class under test

 ̄綄美尐妖づ 提交于 2019-11-30 08:17:34
How I can mock with Mockito other classes in my class which is under test? For example: MyClass.java class MyClass { public boolean performAnything() { AnythingPerformerClass clazz = new AnythingPerformerClass(); return clazz.doSomething(); } } AnythingPerformerClass.java class AnythingPerformerClass { public boolean doSomething() { //very very complex logic return result; } } And test: @Test public void testPerformAnything() throws Exception { MyClass clazz = new MyClass(); Assert.assertTrue(clazz.performAnything()); } Can I spoof AnythingPerformerClass for excluding unnecessary logic from