How to programmatically enable assert?

Try ClassLoader loader = getClass().getClassLoader(); setDefaultAssertionStatus(true); or ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); EDIT: based on the comments ClassLoader loader = ClassLoader.getSystemClassLoader(); loader.setDefaultAssertionStatus(true); Class<?> c = loader.loadClass(“MyClass”); MyClass myObj = (MyClass) c.newInstance(); public class MyClass { private static final int foo[] = new int[]{4,5,67}; MyClass() { assert foo.length == 10; } }

Unit testing: Is it a good practice to have assertions in setup methods?

Instead of assertions in the setup to check the result, I used a simple test (a test method along the others, but positionned as first test method). I have seen several advantages: The setup keeps short and focused, for readability. The assertions are run only once, which is more efficient. Usage and discussion : For … Read more

Why should I use asserts?

First, the performance difference can be huge. In one project our asserts literally caused a 3x slowdown. But they helped us uncover some really pesky bugs. Which is exactly the point. Asserts are there to help you catch bugs. And because they are removed in release builds, we can afford to put a lot of … Read more

Detecting whether on UI thread in WPF and Winforms

Don’t use if(Dispatcher.CurrentDispatcher.Thread == Thread.CurrentThread) { // Do something } Dispatcher.CurrentDispatcher will, if the current thread do not have a dispatcher, create and return a new Dispatcher associated with the current thread. Instead do like this Dispatcher dispatcher = Dispatcher.FromThread(Thread.CurrentThread); if (dispatcher != null) { // We know the thread have a dispatcher that we … Read more

Java/ JUnit – AssertTrue vs AssertFalse

assertTrue will fail if the second parameter evaluates to false (in other words, it ensures that the value is true). assertFalse does the opposite. assertTrue(“This will succeed.”, true); assertTrue(“This will fail!”, false); assertFalse(“This will succeed.”, false); assertFalse(“This will fail!”, true); As with many other things, the best way to become familiar with these methods is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)