Get name of currently executing test in JUnit 4

JUnit 4.7 added this feature it seems using TestName-Rule. Looks like this will get you the method name: import org.junit.Rule; public class NameRuleTest { @Rule public TestName name = new TestName(); @Test public void testA() { assertEquals(“testA”, name.getMethodName()); } @Test public void testB() { assertEquals(“testB”, name.getMethodName()); } }

differences between 2 JUnit Assert classes

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way. Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, … Read more

How to tell a Mockito mock object to return something different the next time it is called?

You could also Stub Consecutive Calls (#10 in 2.8.9 api). In this case, you would use multiple thenReturn calls or one thenReturn call with multiple parameters (varargs). import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; public class TestClass { private Foo mockFoo; @Before public void setup() { setupFoo(); } @Test … Read more

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

Try Skyscreamer’s JSONAssert. Its non-strict mode has two major advantages that make it less brittle: Object extensibility (e.g. With an expected value of {id:1}, this would still pass: {id:1,moredata:’x’}.) Loose array ordering (e.g. [‘dog’,’cat’]==[‘cat’,’dog’]) In strict mode it behaves more like json-lib’s test class. A test looks something like this: @Test public void testGetFriends() { … Read more

How to get the path of src/test/resources directory in JUnit?

You don’t need to mess with class loaders. In fact it’s a bad habit to get into because class loader resources are not java.io.File objects when they are in a jar archive. Maven automatically sets the current working directory before running tests, so you can just use: File resourcesDirectory = new File(“src/test/resources”); resourcesDirectory.getAbsolutePath() will return … Read more

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

According to the documentation for AndroidJUnit4, The gradle file should contain the following line: androidTestImplementation ‘androidx.test.ext:junit:1.1.1′ Change test class to AndroidJUnit4ClassRunner from AndroidJUnit4 If it still doesn’t work, make sure that you clean and/or rebuild your project. Also you can check the current version directly in Google’s maven repository

Mockito. Verify method arguments

An alternative to ArgumentMatcher is ArgumentCaptor. Official example: ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class); verify(mock).doSomething(argument.capture()); assertEquals(“John”, argument.getValue().getName()); A captor can also be defined using the @Captor annotation: @Captor ArgumentCaptor<Person> captor; //… MockitoAnnotations.initMocks(this); @Test public void test() { //… verify(mock).doSomething(captor.capture()); assertEquals(“John”, captor.getValue().getName()); }

How to mock a final class with mockito

Mockito 2 now supports final classes and methods! But for now that’s an “incubating” feature. It requires some steps to activate it which are described in What’s New in Mockito 2: Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to … Read more

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