junit

Java Thread.currentThread().setUncaughtExceptionHandler() not working with JUNIT?

风格不统一 提交于 2019-12-23 08:59:14
问题 In the example code below, if the testMethod() is run via main() it works as expected but if it is run via JUNIT then MyUncaughtExceptionHandler does not get called. Is there some explanation for this? package nz.co.test; import java.lang.Thread.UncaughtExceptionHandler; import org.junit.Test; public class ThreadDemo { private void testMethod() { Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); Object b = null; // Cause a NPE b.hashCode(); } @Test public

Mocking a class object using Mockito and PowerMockito

ε祈祈猫儿з 提交于 2019-12-23 08:53:15
问题 Is it possible to mock a class object using Mockito and/or PowerMockito? Something like: Class<Runnable> mockRunnableClass = mock(Class<Runnable>.class); 回答1: An alternative to mocking Class might be to use a Factory instead. I know you are concerned about refactoring, but this could be done without changing the public API of the class. You haven't provided much code to understand the class you are trying to test, but here's an example of refactoring without changing the API. It's a trivial

MockitoException - is a *void method* and it *cannot* be stubbed with a *return value*!

天涯浪子 提交于 2019-12-23 08:49:48
问题 I'm using Mockito for unit testingand I'm getting the following exception. org.mockito.exceptions.base.MockitoException: `'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*! Voids are usually stubbed with Throwables: doThrow(exception).when(mock).someVoidMethod(); *** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because: 1. The method you are trying to stub is *overloaded*. Make

Android JUnit4 testing

ⅰ亾dé卋堺 提交于 2019-12-23 07:47:35
问题 I want to run some JUnit4 tests. The code relies on some Android libraries(Android XML parser), but does not create any activites and so on. When I try to run tests I got that an Android class that I need was not found. Is there any way to run JUnit4 tests with Android code, not to test activity but to test code with some libraries. 回答1: There is no way (that I'm aware of) to use JUnit4 on Android. It does support JUnit3 though, if that's an option for you? Alternatively, you could use

Solving java classpath hell in legacy junit test suite

旧巷老猫 提交于 2019-12-23 07:45:46
问题 Suppose I have a legacy JUnit test suite that includes the following tests: public class AwesomeTest { public void testBusinessLogic() { ... [awesome mocking library] ... } } public class AmazingTest { public void testBusinessProcess() { ... [amazing xml operation] ... } } Now assume that Awesome Mocking library relies on Awesome BCEL bytecode generation library which contains the class org.useful.XMLClass and this library has version 1 of XMLClass. Now assume that Amazing Xml operation

Solving java classpath hell in legacy junit test suite

回眸只為那壹抹淺笑 提交于 2019-12-23 07:45:45
问题 Suppose I have a legacy JUnit test suite that includes the following tests: public class AwesomeTest { public void testBusinessLogic() { ... [awesome mocking library] ... } } public class AmazingTest { public void testBusinessProcess() { ... [amazing xml operation] ... } } Now assume that Awesome Mocking library relies on Awesome BCEL bytecode generation library which contains the class org.useful.XMLClass and this library has version 1 of XMLClass. Now assume that Amazing Xml operation

How do I create Android test suite which only runs specified tests in one or more classes?

最后都变了- 提交于 2019-12-23 07:28:27
问题 Can someone shed some light on how to organize tests in test suites, using JUnit in Android? I find almost all examples to be non-working, and I'm wondering what it is that I'm not getting. I've made a small example with an AndroidTestCase class containing a couple of tests, and a test suite which includes all the tests in the package. This works (apparently): The test case class containing the tests: public class ArithmeticsTest extends AndroidTestCase { SomeClass sctest; protected void

How do I create Android test suite which only runs specified tests in one or more classes?

时光怂恿深爱的人放手 提交于 2019-12-23 07:28:10
问题 Can someone shed some light on how to organize tests in test suites, using JUnit in Android? I find almost all examples to be non-working, and I'm wondering what it is that I'm not getting. I've made a small example with an AndroidTestCase class containing a couple of tests, and a test suite which includes all the tests in the package. This works (apparently): The test case class containing the tests: public class ArithmeticsTest extends AndroidTestCase { SomeClass sctest; protected void

Using DateFormat.getDateTimeInstance().format(date);

狂风中的少年 提交于 2019-12-23 07:04:19
问题 When running some tests I came across the following issue. When using: private String printStandardDate(Date date) { return DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT).format(date); } I found this produced different formats of Date depending on the location the tests where run from. So locally in windows / eclipse I got a result: 04/02/12 18:18 but on the Linux box in America I get 2/4/12 6:18 PM This causes my Tests/Build to fail: expected:<[04/02/12 18:18]> but was:<

Mockito mocked object is assigned a null value from method calls inside a method

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 06:25:06
问题 I have a class that I need to test with mockito. Below is the class and the Mockito Test. dbBuilder.parse(file) always returns null because it calls several classes which, in turn, call several methods in a jar file. Even though I mocked all of them it always returns null. I couldn't track where the null value is coming from. I tried to suppress the methods but still no use. Since this method call returns null, the doc value is null. So the doc calls the getElementsByTagName method and