Arefe
Arefe

Reputation: 12461

Testing of the Spring back-end

I'm new in Spring framework and need to test a some back-end software. The back-end is written in three layers - i) dao, ii) Services and iii) controllers. The 'dao' is the nearest layer of the database and all the SQL queries are there. The 'services' layer is used for making JSON of the extracted data from 'dao' layer. The 'controllers' layer is used for throwing the JSON string to the front-end.

The architecture is [ database <-> dao <-> services <-> controllers <-> front-end ]

The sample code for the three layers are the following ->

GDao.java
==============

@Repository
public class GarageDao {

    private JdbcTemplate m_oJdbcTemplateObj;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.m_oJdbcTemplateObj = new JdbcTemplate(dataSource);
    }

    /**
     * Query data base to get all the parking site id, identifier, description, and shape
     * @return list of map
     */

    public List<Map<String,Object>> getAllLocation() {
        List<Map<String, Object>> results; 
        try{
            // 
        } 
        catch(Exception e){
            // 
        }   
        return results;
    }
}

GServices.java
===================

@Service("m_oGarageService")
public class GarageServices {

    @Autowired
    private GarageDao m_oGarageDao;

    public String getAllLocation() {

        List< Map<String, Object> > results = m_oGarageDao.getAllLocation();

        try {

            if (null != results) {

                JSONObject featureCollection = new JSONObject();
                featureCollection .put("type", "FeatureCollection");


  JSONArray featureList = new JSONArray();

                        for (Map<String, Object> m : results) {
           // some code to insert data into featureList  
    }
                // return JSON data String 
                return featureCollection.toString();
            }      
        } 

        catch (JSONException e) {
            // some code
        }
        return null;
    }
}

GControllers.java
======================

@Controller
@RequestMapping("/garage")

public class GarageController {

    @Autowired
    private GarageServices m_oGarageService;


    @RequestMapping("/getall")

    public @ResponseBody ResponseEntity<String> getAllLocation(){
        HttpHeaders ResultHeader = new HttpHeaders();
        ResultHeader.set("Access-Control-Allow-Origin", "*");
        ResultHeader.set("Content-Type", "application/json");
        String result = m_oGarageService.getAllLocation();

        if(null ==  result){
            //  some code
         }
         return new ResponseEntity<String>(result, ResultHeader, HttpStatus.OK); 
     }
}

How can I start write test for the back-end ? Thanks.

Upvotes: 0

Views: 548

Answers (1)

First, I'd use Spring Data repositories instead of custom DAO code. Then write your beans to use constructor injection and simply test them with mocks the same way you would anything else, with Mockito or Spock.

Upvotes: 1

Related Questions