junit4

Why cannot I import AndroidJUnit4 and ActivityTestRule into my unit test class?

妖精的绣舞 提交于 2019-11-27 01:12:16
问题 I'm having trouble importing some of the Android UI testing framework clases - I just can't figure out what is going wrong! This is my class: @RunWith(AndroidJUnit4.class) @LargeTest public class ExampleUnitTest { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); @Test public void listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())); } } But for some reason I get errors 'cannot find symbol ActivityTestRule'

Mockito: Mock private field initialization

北城以北 提交于 2019-11-27 01:00:08
How I can mock a field variable which is being initialized inline? e.g. class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock person.someMethod() while testing method - Test#testMethod. for which I need to mock initialization of person variable. Any clue? EDIT: I'm not allowed to modify Person class. Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util.reflection.Whitebox; //... @Mock private Person mockedPerson; private Test underTest; // ... @Test public

Maven 3 and JUnit 4 compilation problem: package org.junit does not exist

佐手、 提交于 2019-11-27 00:49:25
问题 I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it? The problem manifests itself in a compilation failure: "package org.junit does not exist". This is because of the import statement in my source code. The correct package name in JUnit 4.* is org.junit.* while in version 3.* it is junit.framework.* I think I have found documentation on the root of the problem on

How do I assert an Iterable contains elements with a certain property?

橙三吉。 提交于 2019-11-27 00:45:37
Assume I want to unit test a method with this signature: List<MyItem> getMyItems(); Assume MyItem is a Pojo that has many properties, one of which is "name" , accessed via getName() . All I care about verifying is that the List<MyItem> , or any Iterable , contains two MyItem instances, whose "name" properties have the values "foo" and "bar" . If any other properties don't match, I don't really care for the purposes of this test. If the names match, it's a successful test. I would like it to be one-liner if possible. Here is some "pseudo-syntax" of the kind of thing I would like to do. assert

How to write JUnit test with Spring Autowire?

寵の児 提交于 2019-11-26 23:06:26
问题 Here are the files that I use: component.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http:/

JUnit tests always rollback the transactions

北城余情 提交于 2019-11-26 22:45:28
问题 I'm running a simple JUnit test agains an application DAO. The problem is that I always get: javax.persistence.RollbackException: Transaction marked as rollbackOnly The JUnit test is: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:com/my/app/context.xml"} @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) @Transactional public class PerformanceTest { @Test @Transactional(propagation= Propagation.REQUIRES_NEW)

java.lang.Exception: No runnable methods exception in running JUnits

三世轮回 提交于 2019-11-26 22:39:31
问题 I am trying to run the JUnit on my Linux command prompt /opt/junit/ contains the necessary JARS(hamcrest-core-1.3.jar and junit.jar) and class files and I am using the following command to run the JUnit: java -cp hamcrest-core-1.3.jar:junit.jar:. org.junit.runner.JUnitCore TestRunner TestJunit class: import org.junit.Test; import static org.junit.Assert.assertEquals; public class TestJunit { @Test public void testAdd() { String str= "Junit is working fine"; assertEquals("Junit is working fine

Don't let test stop on failure

血红的双手。 提交于 2019-11-26 22:08:41
问题 I'm looking for the best practice for following (simplified) scenario: @Test public void someTest() { for(String someText : someTexts) { Assert.true(checkForValidity(someText)); } } This test iterates through x-thousands of texts and in this case I don't want it to be stopped for each failure. I want the errors to be buffered and in case of error(s) to fail at the end. Has JUnit got something on board for for my aim? 回答1: First of all, it's not really the correct way to implement this. JUnit

Can't run JUnit 4 test case in Eclipse Android project

喜夏-厌秋 提交于 2019-11-26 21:56:00
I am new to Java and am trying to run a unit test on a class I am writing. Eclipse (3.5) created the unit test class for me and added Junit4 to my class path. My Class: public class DistanceUtil { public static double metersToMiles( double meters ) { return 0; } public static double metersToKilometers( double meters ) { return 0; } } My unit test: public class DistanceUtilTest { @Test public final void testMetersToMiles() { fail("Not yet implemented"); // TODO } @Test public final void testMetersToKilometers() { fail("Not yet implemented"); // TODO } } When I right click on the unit test and

No suitable classloader found for grab

时光总嘲笑我的痴心妄想 提交于 2019-11-26 21:29:43
问题 I have this at the beginning of a class: @Grab(group = 'org.ccil.cowan.tagsoup', module = 'tagsoup', version = '1.2') class MyClass{... I'm trying to unit test this class, but whenever I try to run JUnit 4 tests, I get this error: Caused by: java.lang.RuntimeException: No suitable ClassLoader found for grab at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect