Looking for an example of a custom SynchronizationContext (Required for unit testing)

This one was written by me some time ago, no issues with copyright, no guarantees either(the system didn’t go into production): using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Windows.Threading; namespace ManagedHelpers.Threads { public class STASynchronizationContext : SynchronizationContext, IDisposable { private readonly Dispatcher dispatcher; private object dispObj; private readonly Thread mainThread; public … Read more

How to test a Jersey REST web service?

If you want to test using the URL, then you will need to start a server from your test. You can explicitly start an embedded server, which is pretty common for tests. Something like public class MyResourceTest { public static final String BASE_URI = “http://localhost:8080/api/”; private HttpServer server; @Before public void setUp() throws Exception { … Read more

Run JUnit tests with SBT

Finally, I’ve discovered, that I have to add the following settings to the subproject: lazy val webapp = project settings( Seq( projectDependencies ++= Seq( …. “org.scalatest” %% “scalatest” % “2.2.2” % Test, “junit” % “junit” % “4.11” % Test, crossPaths := false, “com.novocode” % “junit-interface” % “0.11” % Test ) ): _* ) It is … Read more

Getting context in AndroidTestCase or InstrumentationTestCase in Android Studio’s Unit Test feature

Updated – Please use Espresso for writing instrumentation tests Newer Examples: I got these working without deploying to a device. Put the tests in the /src/main/test/ folder. Here are newer examples, I took your examples and tested them in my own temporary test project. I ran the tests via command line: ./gradlew clean test. Please … Read more

How can I write a unit test to determine whether an object can be garbage collected?

This is what I normally do: [Test] public void MyTest() { WeakReference reference; new Action(() => { var service = new Service(); // Do things with service that might cause a memory leak… reference = new WeakReference(service, true); })(); // Service should have gone out of scope about now, // so the garbage collector can … Read more

Unit Testing in Xcode, does it run the app?

The application is actually run but there is a trick you can use to prevent it from running. int main(int argc, char* argv[]) { int returnValue; @autoreleasepool { BOOL inTests = (NSClassFromString(@”SenTestCase”) != nil || NSClassFromString(@”XCTest”) != nil); if (inTests) { //use a special empty delegate when we are inside the tests returnValue = UIApplicationMain(argc, … Read more