Mockito + PowerMock LinkageError while mocking system class

I’ve got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure = new AllMeasuresData(); assertEquals(measure.getMeasures(), null); HashMap<String, Measure> map = new HashMap<String, Measure>(); measure.setMeasures(map); assertEquals(measure.getMeasures(), map); measure.setMeasures(null); assertEquals(measure.getMeasures(), null); } @Test public void testAllMeasuresData() throws IOException { ClassLoader loader … Read more

Is Java’s assertEquals method reliable?

I know that == has some issues when comparing two Strings. It seems that String.equals() is a better approach. Well, I’m doing JUnit testing and my inclination is to use assertEquals(str1, str2). Is this a reliable way to assert two Strings contain the same content? I would use assertTrue(str1.equals(str2)), but then you don’t get the … Read more

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

I’m using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I’ve created a custom matcher that looks like the following: public static class MyMatcher extends TypeSafeMatcher<String> { @Override protected boolean matchesSafely(String s) { /* implementation */ } @Override public void describeTo(Description description) { /* implementation */ } @Override protected void describeMismatchSafely(String item, Description mismatchDescription) { /* implementation */ … Read more

How do I assert my exception message with JUnit Test annotation?

I have written a few JUnit tests with @Test annotation. If my test method throws a checked exception and if I want to assert the message along with the exception, is there a way to do so with JUnit @Test annotation? AFAIK, JUnit 4.7 doesn’t provide this feature but does any future versions provide it? … Read more

How to run test methods in specific order in JUnit4?

I want to execute test methods which are annotated by @Test in specific order. For example: public class MyTest { @Test public void test1(){} @Test public void test2(){} } I want to ensure to run test1() before test2() each time I run MyTest, but I couldn’t find annotation like @Test(order=xx). I think it’s quite important … Read more

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

What is the main difference between @Before and @BeforeClass and in JUnit 5 @BeforeEach and @BeforeAll @After and @AfterClass According to the JUnit Api @Before is used in the following case: When writing tests, it is common to find that several tests need similar objects created before they can run. Whereas @BeforeClass can be used … Read more