Using reflection to change static final File.separatorChar for unit testing?

From the documentation for Field.set: If the underlying field is final, the method throws an IllegalAccessException unless setAccessible(true) has succeeded for this field and this field is non-static. So at first it seems that you are out of luck, since File.separatorChar is static. Surprisingly, there is a way to get around this: simply make the … Read more

How to test methods that call System.exit()?

Indeed, Derkeiler.com suggests: Why System.exit() ? Instead of terminating with System.exit(whateverValue), why not throw an unchecked exception? In normal use it will drift all the way out to the JVM’s last-ditch catcher and shut your script down (unless you decide to catch it somewhere along the way, which might be useful someday). In the JUnit … Read more

How to write this EF Mock setup code as a reusable Generic Boilerplate?

Tim Larson already offered a great solution for this boilerplate code in his blog: public static class DbSetMocking { private static Mock<DbSet<T>> CreateMockSet<T>(IQueryable<T> data) where T : class { var queryableData = data.AsQueryable(); var mockSet = new Mock<DbSet<T>>(); mockSet.As<IQueryable<T>>().Setup(m => m.Provider) .Returns(queryableData.Provider); mockSet.As<IQueryable<T>>().Setup(m => m.Expression) .Returns(queryableData.Expression); mockSet.As<IQueryable<T>>().Setup(m => m.ElementType) .Returns(queryableData.ElementType); mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()) .Returns(queryableData.GetEnumerator()); return … Read more

Xamarin (Android) Unit Tests in Visual Studio 2017

There are three basic levels of testing: The classic unit testing of pure .Net/Mono code via xUnit|NUnit Nothing new here, this is the same testing that .Net programmers have been doing all long and has nothing to do with the Xamarin platform frameworks Note: These tests are totally independent of Xamarin.Android|iOS|Mac On platform testing (including … Read more

how do I mock sqlconnection or should I refactor the code?

SqlBulkWriter class is tightly coupled to implementation concerns which make it difficult to test the class in isolation. Knowing the connection string is not really a concern of that class and can be delegated out to another service. Something like public interface IDbConnectionFactory { IDbConnection CreateConnection(); } and its implementation for your class would look … Read more

Is it a good way of unit testing to use another, tested function to make preparations for the actual test?

In a perfect world, every unit test can only be broken in single way. Every unit test “lives” in isolation to every other. Your addNewFruit test can be broken by breaking isInFruitsList – but luckily, this isn’t a perfect world either. Since you already tested isInFruitsList method, you shouldn’t worry about that. That’s like using … Read more

When to use mock objects in unit tests

As you’re writing new code (along with the new unit tests) or refactoring existing code, you want to be able to run the unit tests over and over to be reasonably confident existing functionality was not broken. Therefore, the unit tests must be stable and fast. Suppose the class to be tested depends on some … Read more