-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from sialcasa/#134_fluentViewLoader_mocking
Add a unit test that shows how you can mock the view loader.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
mvvmfx/src/test/java/de/saxsys/mvvmfx/internal/viewloader/MockableViewLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package de.saxsys.mvvmfx.internal.viewloader; | ||
|
||
import de.saxsys.mvvmfx.ViewTuple; | ||
import org.junit.Test; | ||
|
||
import de.saxsys.mvvmfx.FluentViewLoader; | ||
import de.saxsys.mvvmfx.internal.viewloader.example.TestFxmlView; | ||
import de.saxsys.mvvmfx.internal.viewloader.example.TestViewModel; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* The purpose of this test is to check that mocking of view loading is possible. | ||
* | ||
* In theory loading a view is something that involves the environment of the application and therefore | ||
* goes beyond the scope of a Unit-Test. So in most cases the developer shouldn't need to test the view loading with a unit test. | ||
* | ||
* The MVVM pattern supports this by saying that the loading of another view is done in the View component and | ||
* <strong>not</strong> in the ViewModel and the View is not a target of unit-tests in MVVM. | ||
* | ||
* Only the conditions under which a view has to be loaded should be defined (and unit-tested) in the ViewModel. | ||
* | ||
* The consequence of these statements is that testing the loading of a view isn't a first-class problem with MVVM. | ||
* It should only be needed in some rare corner-cases. | ||
* | ||
* The API of MvvmFX reflects this: The methods in {@link FluentViewLoader} to load a view are static for a better usability. | ||
* They are not optimized for unit-testing and mocking. | ||
* | ||
* This test will show that mocking the loading process is still possible when with a little bit of extra code. | ||
* | ||
*/ | ||
public class MockableViewLoaderTest { | ||
|
||
public static class MyApplication { | ||
|
||
public FluentViewLoader.FxmlViewStep<TestFxmlView, TestViewModel> builder = | ||
FluentViewLoader.fxmlView(TestFxmlView.class); | ||
|
||
|
||
public ViewTuple<TestFxmlView, TestViewModel> viewTuple; | ||
|
||
public void methodToTest() { | ||
viewTuple = builder.load(); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void test() { | ||
MyApplication application = new MyApplication(); | ||
|
||
FluentViewLoader.FxmlViewStep mock = mock(FluentViewLoader.FxmlViewStep.class); | ||
|
||
ViewTuple<TestFxmlView, TestViewModel> viewTupleMock = mock(ViewTuple.class); | ||
when(mock.load()).thenReturn(viewTupleMock); | ||
|
||
|
||
application.builder = mock; | ||
application.methodToTest(); | ||
|
||
assertThat(application.viewTuple).isNotNull().isSameAs(viewTupleMock); | ||
} | ||
|
||
|
||
} |