I am new to using Mockito and I am running through an example test class written in our GWT project.
At some places ,in order to get a Mock we used Mockito.mock(SecurityDao.class)
but in other places in the same test class we instantiated other classes using the "new" keyword.
I think that in order to mock a class i need to pass in the interface as the parameter to Mockito.mock ,and if my class does not implement an interface then i need to use the "new" keyword to instantiate the class.
Is this correct?When should i really use Mockito.mock??
Thanks
Always use Mockito#mock() when creating an object other than that under test. Mockito can create mocks for interfaces and classes.
Related
If the Application has a Custom Application object. It is needed to annotate this with #HiltAndroidApp
ex:
#HiltAndroidApp
class AppCore: Application
Appcore has some initialization logic which is needed for the app to run
Now in the Instrumentation tests We also need to Extend the custom Application object.
#CustomTestApplication(AppCore::class)
interface HiltTestApplication
This gives an error #CustomTestApplication value cannot be annotated with #HiltAndroidApp
Is there any other way of using HILT in instrumentation tests with custom Application objects
public abstract interface HiltTestApplication {
^
#CustomTestApplication value cannot be annotated with #HiltAndroidApp. Found: AppCore
As suggested in the issue tracker. Can you abstract your initialization logic into a base class, say BaseAppCore : Application then in your prod application extend it #HiltAndroidApp AppCore : BaseAppCore and then for tests make Hilt generate a test app based on your abstract one, #CustomTestApplication(BaseAppCore::class) interface AppCoreTestApplication. It might be best to file this issue in https://github.com/google/dagger/issues
You will need to create a new class with the #HiltAndroidApp annotation, which would be different from the one you will use in your tests.
open class AppCore: Application
{
// Existing code.
}
#HiltAndroidApp
class ProdAppCore : AppCore
{}
#CustomTestApplication(AppCore::class)
interface HiltTestApplication
If you are using Robolectric, you can set:
application = $packageName.HiltTestApplication_Application
And in your AndroidManifest.xml, set:
<application
android:name="$packageName.ProdAppCore"
where $packageName is the package where ProdAppCore and HiltTestApplication class have been defined.
This is also discussed here: https://github.com/google/dagger/issues/2033
I am building an app that implements custom docblock annotations using the Doctrine Annotations library.
For PHPUnit testing, is it possible to create a mocked class that has mock docblocks?
From this answer, I learned how to mock a class, like so:
$foo = $this->getMockBuilder('nonexistant')
->setMockClassName('TestClass')
->getMock();
Is there a way to mock a docblock? Building on the class example, Something like this is what I imagine:
$foo = $this->getMockBuilder('nonexistant')
->setMockClassName('TestClass')
->setMockClassDocblock('/** #SomeAnnotation("foo") */')
->getMock();
If not - is there anything I can do besides just creating actual test classes?
There is no way to mock a docblock. If you can make the case that there should be then please open a ticket.
I'm new to the testing environment,i have small confusion in adding the class file,i want to test the restful web services
Which class file to include in the Class under Test in the junit test case creation?
Whether the dao class or bean class or service class.
It doesnt matter, you could skip it. The class-under-test is for writing a single unit test for a single class, where the wizard creates the test method stubs for the methods of the class for you.
When you want to test a REST service, you don't need that and could skip that part. Just create the test and name the test method according to the use case or scenario you want to test.
Using JUnit4, the test itself is even a plain java class, the only thing that makes it special are #Test annotated methods. So you don't even need that entire wizard.
But in case you want to UnitTest the service, you select the service class itself (i.e. the Jax-RS service class annotated with #Path)
I am trying to implement gwt editor framework.
I have created a driver as follows:
final Driver driver = GWT.create(Driver.class);
ABC is my class and I am passing object of my ABC class to
driver.edit();
function.
Now instead of class I want to use Interface.
But since we cant create instance of an interface how shall i proceed
for the same?
Can we use interfaces in above mentioned case ?
The Editor framework won't ever instantiate anything (other than its own internal things, of course), so using interfaces is safe, and just like with classes.
Try it, it just works.
Hi I am new to AspectJ and I would like to find out if creating variants of a class using Aspects - I will create another instance of the class as well?
I am guessing that the question is, if I am adding aspects would a new class be created.
The answer is no, as the weaving, either when compiling or at run-time, using AspectJ, will add the changes to the classes that are affected by the aspects, so there is no new class created, it is just that the byte code for the original class and the final class are different.
What do you mean by variants?
If you are asking if AspectJ instantiates copies of your class, the answer is no.
AspectJ uses a design pattern called proxy to intercept calls to your class.