Laravel Mockery Integration Test - laravel

I am a little lost on how to perform integration test using mockery.
I have the following classes:
TeacherController
TeacherManager - Interface
TeacherManagerImpl - Implementation
When it comes to mockery / PHPUnit, how do I call a method from my interface? It says that the interface can not be instantiated. I know it can't but how can I inject it into the test class or should I just do new on the implementation. Doing a new on the implementation just does not seem right to me.

Thanks to Kindari in the Laravel IRC chat room
In the Test setup method just bind the interface to the implementation and then set a private member variable using App::make. See below.
App::bind('FooInterface', 'FooImplementation');
$foo = App::make('FooInterface');
also app() is a shortcut to App::make

Related

SpringBoot Kotlin - Could not compute caller for function: private final fun

Currently, I try to use SpringBoot in combination with Kotlin and creating an interface with some methods with default behavior.
When all the methods are public in the interface everything is fine. But as soon I try to make a helper method private I get the following example error message:
Could not compute caller for function: private final fun [....] member = null
Java doesn't allow private members in interface and kotlin based on java so they are same one programming language like it said in oracle(java) doc Java Access Control :
"The Java programming language provides mechanisms for access control,
to prevent the users of a package or class from depending on
unnecessary details of the implementation of that package or class."
Access control is all about hiding implementation details. An interface has no implementation to hide.

Autowired not working with Azure function

I am quit new in Azure function.
I found it as a interesting topic.right now,I have already develped azure function and works fine.
BUT my story will not end here. In the Function Method, I am trying to Autowiring my Repository class in spring in order to access to my DB layer.
but it gives me a null pointer exception. means that , "#Autowired" annotation is not working and not initiate my HotelController Class.
Any Idea , why I am not able to get the instance in Azure function?
I think you should take a look at:
spring-cloud-function-adapter-azure
This project provides an adapter layer for a Spring Cloud Function application onto Azure. You can write an app with a single #Bean of type Function and it will be deployable in Azure if you get the JAR file laid out right.
There is an AzureSpringBootRequestHandler which you must extend, and provide the input and output types as annotated method parameters (enabling Azure to inspect the class and create JSON bindings). The base class has two useful methods (handleRequest and handleOutput) to which you can delegate the actual function call, so mostly the function will only ever have one line.
And the sample that shows how to use it.
Hope it helps!

Executing extension before SpringExtension

I'm trying to implement integration testing in my app and have test class like that:
#ExtendWith(value={MyDockerExtension.class})
#ExtendWith(value={SpringExtension.class})
#WebAppConfiguration
#ContextConfiguration(classes={...})
#TestInstance(TestInstance.LifeCycle.PER_CLASS)
public class TestClass{ ... }
Is there any way to make MyDockerExtension execute some code, before whole SpringExtension start working and generate whole Context with Configurationc classes?
I've heard that order in which we declare extensions is the key, but sadly MyDockerExtension that implements BeforeAllCallback, AfterAllCallback executes right before test method and after whole context is loaded. In that situation it's to late to start containers with docker, becuase since whole context is loaded my app already tried to connect to the container.
At first I was skeptical about the order being fixed but you're correct:
Extensions registered declaratively via #ExtendWith will be executed in the order in which they are declared in the source code.
Regarding the MyDockerExtension, you may want to look at the extension point TestInstancePostProcessor, which is called before #BeforeAll. SpringExtension implements it and I guess it's there where it sets up the application context. If you also implement it, you should be able to act before it does.

Testing spring repositories

In the Spring Data I have found very helpful interface called JpaRepository. Because I need more functionality I decided to create my own interface of repository:
public interface BaseRepository<T, ID extends Serializable>
extends JpaRepository<T, ID> {
public <TA, TV> int deleteBy(SingularAttribute<T, TA> attr, TV val);
}
As you can see this is a generic interface. It works fine, but I would like to know how I can test it? Of course I can write integration test for each concrete repository but I am looking for better way.
As usual with testing, you should make sure you know what you're testing. Find answers to these questions:
Do you want to test the underlying database?
Do you want to test the Spring Data repository connector for this respository?
Do you want to test whether your code calls the correct methods on the interface?
Doing #1 is useless: The database vendor has already run thousands of tests on its product. There is rarely a reason to do this effort again.
Doing #2 is useless unless you suspect a bug in the code for Spring Data.
Which leaves us with #3. Use a mocking framework to make sure the method is called at the appropriate places (and maybe check the arguments, too).
That way, you can make sure your code behaves correctly.
If you notice the framework throwing errors or you notice that objects aren't deleted correctly, you can add more tests. But most of the time, this won't happen because of bugs in the database or Spring Data. Instead, your code won't call deleteBy() or it will call the method with the wrong arguments.

Unit Testing While Using Entity Framework

I have a web application that has been created using MVC 3 and Entity Framework. I would like to start using unit testing, but so far I have not been able to run any unit tests due to the way the system talks to the database.
I have a BaseController, which defines a DataContext as a variable:
public class BaseController : Controller
{
public Models.MyEntities DataContext = new Models.MyEntities();
Each controller then inherits the BaseController, thereby making the DataContext variable available to all controllers without redefining it:
public class ErrorController : BaseController
{
When I run a unit test, I receive an error about the object reference not being set to an instance of an object, each time referring to the line where I access DataContext.
Most references to unit testing say you should be abstracting your database layer, and using fake data for testing. This seems counter-intuitive to me, but that is a different discussion.
My question is - is it possible to use unit testing with the system configured the way it is? I am open to using any testing framework available, either MSTest, NUnit / XUnit etc.

Resources