How to modularize when unit tests depend on other units?

I am trying to retrospectively test a fairly complex application that utilizes MVC. I know that retrospectively applying unit testing is not ideal, but I still believe that by refactoring existing code Possibly. In most cases, it does not depend on other units, that is, unit tests a unit, that is, the view depends on the model.

In this case, what is the best way to unit test? Is it better to use a real model or create a simulated model?

The problem with using the real model in my case is that the model relies on other response classes that get data from XML, so there is a series of dependencies. This model has a lot of data, so it will be much easier to use it , But maybe I missed this.

For the sake of brevity, I have provided a UML of the application.

**Edit****

Ok, if I am correct, is it better to create mock data in mock class? For example, I have a mock class “MockPlaylistPanelModel”, which creates the data needed for the View class “PlaylistPanel” to run, without errors:

class MockPlaylistPanelModel extends Mock implements IPlaylistPanelModel
{
/**
* Return all playlist items
* @public
*/
public function get mainPlaylistItems():Vector.
{
var playData:Vector. = new Vector.;
var playlistResp:PlaylistData = new PlaylistData(0, "", "", 0, 0, 0, 0);
playData.push(playlistResp);
return playData;
}

}

To backtrack unit testing into an existing application, you usually need to change the application code to support unit testing (as you mentioned, you may need to perform some refactoring) Of course, the risk here is that changes to the application introduce errors, and without some testing, these errors cannot be prevented.

So, a wise approach is to target some of your key Use cases for some system-level testing. This is a kind of “test scaffolding” around your application, which means you can start introducing lower-level tests more safely, while modifying the application to make it easier to test Reduce the risk of introducing errors. Once in place, you can introduce a strategy where developers must write tests against any code they change before making changes-this allows you to organically add a set of automated tests around the application.

< p>I strongly recommend mastering Working Effectively with Legacy Code – this excellent book covers all aspects of introducing testing into existing applications. This is a useful technique. These applications have almost no automated testing.

The question about whether simulation data should be created in the simulation class for testing, this is a method that can be used when injecting the test version of the object , But may not be the best. By using a mocking framework like Mockito, you can easily create mock objects with well-defined behavior. In your case, you can use Mockito to create mock model implementations, and then add mock models Inject any objects that depend on it.

I am trying to retrospectively test a fairly complex application that utilizes MVC. I know that applying unit testing retrospectively is not ideal , But I still believe that it is possible to refactor the existing code. In most cases, a unit is not dependent on other units, that is, a unit is tested, that is, the view depends on the model.

In this In this situation, what is the best way to unit test? Is it better to use a real model or create a simulated model?

The problem with using the real model in my case is that the model relies on other response classes that get data from XML, so there is a series of dependencies. This model has a lot of data, so it will be much easier to use it , But maybe I missed this.

For the sake of brevity, I have provided a UML of the application.

**Edit****

Ok, if I am correct, is it better to create mock data in mock class? For example, I have a mock class “MockPlaylistPanelModel”, which creates the data needed for the View class “PlaylistPanel” to run, without errors:

class MockPlaylistPanelModel extends Mock implements IPlaylistPanelModel
{
/**
* Return all playlist items
* @public
*/
public function get mainPlaylistItems():Vector.
{
var playData:Vector. = new Vector.;
var playlistResp:PlaylistData = new PlaylistData(0, "", "", 0, 0, 0, 0);
playData.push(playlistResp);
return playData;
}

}

To backtrack unit testing into an existing application, you usually need to change the application code to support unit testing (as you mentioned, you may need to perform some refactoring). Of course, the risk here is the introduction of changes to the application If you do not perform some tests, you cannot prevent these errors.

So, a wise approach is to conduct some system-level tests for some of your key use cases. This is a kind of “Test scaffolding” for your application, which means you can start introducing lower-level tests more safely, while reducing the risk of introducing errors when modifying the application to make it easier to test. Once in place, you can Introduce a strategy where developers must write tests for any code they change before making changes – this allows you to organically add a set of automated tests around the application.

I strongly recommend mastering Working Effectively with Legacy Code – This excellent book covers various useful techniques for introducing testing into existing applications that have almost no automated testing.

Question about whether simulation data should be created in a simulation class for testing , This is one method you can take when injecting a test version of an object, but it may not be the best. By using a mocking framework like Mockito, you can easily create mock objects with well-defined behavior. In your In this case, you can use Mockito to create a simulation model implementation, and then inject the simulation model into any objects that depend on it.

Leave a Comment

Your email address will not be published.