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 to tell a Mockito mock object to return something different the next time it is called?

So, I’m creating a mock object as a static variable on the class level like so… In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I’m having is that it seems I need to rebuild the mocks to get … Read more

Get name of currently executing test in JUnit 4

In JUnit 3, I could get the name of the currently running test like this: public class MyTest extends TestCase { public void testSomething() { System.out.println(“Current test is ” + getName()); … } } which would print “Current test is testSomething”. Is there any out-of-the-box or simple way to do this in JUnit 4? Background: … Read more

How to do a JUnit assert on a message in a logger

I have some code-under-test that calls on a Java logger to report its status. In the JUnit test code, I would like to verify that the correct log entry was made in this logger. Something along the following lines: methodUnderTest(bool x){ if(x) logger.info(“x happened”) } @Test tester(){ // perhaps setup a logger first. methodUnderTest(true); assertXXXXXX(loggedLevel(),Level.INFO); … Read more

Testing two JSON objects for equality ignoring child order in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 7 months ago. The community reviewed whether to reopen this question 7 months ago and left it closed: Original close reason(s) were not resolved Improve … Read more

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using ReflectionTestUtils.setField but the bean instance that I wish to inject is actually a proxy … Read more

AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?

For my instrumentation tests I was using @RunWith(AndroidJUnit4.class) from import androidx.test.runner.AndroidJUnit4; in order to establish my test cases. Now this line gets marked as deprecated with the hint to use AndroidJUnit4 from import androidx.test.ext.junit.runners.AndroidJUnit4 However if I try to import AndroidJUnit4 from the named package I get the error, that ext can not be resolved. … Read more