Dhia uas xeem coj – nws ua haujlwm li cas?

Spring with testing framework

Dhia nrog soj ntsuam cov moj khaum – Nws yuav ua li cas?

Txheej txheem cej luam

Chav tsev kuaj thiab muaj kev koom ua ke los soj ntsuam mob nyav theem hauv ib software tsim lub neej nws. Txawm li cas los, lwm yam kev yuav tiv thaiv tau pub thiab tag nrho tej zaum ntawm no ob xeem tas le caag. Tus Caij nplooj ntoos hlav moj khaum yuav pab ib enterprise overcome tau txoj kev muab lub moj khaum ready made rau automation los soj ntsuam. Ob chav tsev thiab kev koom ua ke los soj ntsuam tau cov dependencies uas tej zaum yuav tsis tau fulfilled yog lub software yog nyob rau hauv ib lub xeev mature. Qhia hais tias cov teeb meem tau hais paub ua ntej, Caij nplooj ntoos hlav moj khaum muab tej simulations los yog mock cov zauv uas pab qhauj teb chaws thiab kev koom ua ke los soj ntsuam xyuas kom meej tias cov software yog yuam kev tsis pub.








Lub moj khaum ntawd yog dab tsi?

IB soj ntsuam cov moj khaum qhia raws li kev cai los yog cov kev cai rau automation cov kuaj software. Tom qab lub moj khaum xeem tsi muaj Individual. Following a testing framework may yield many benefits such as reuse of code, reduced script maintenance cost and higher portability. There are a number of testing frameworks available such as Linear scriptingrecord and playback and the test library architecture framework.

What is a Spring framework?

Let us think of a caij nplooj ntoos hlav like the framework of a housing community. When the builder of a housing community is building the second or third housing community, the builder does not need to know about building everything from scratch because he or she has some experience already. It is about building on the experience and learning from new experience. As the builder goes on building new communities, new lessons such as costing and materials are learnt. When you buy a house, you do not need to build it from the scratch. You can customize it in the way you like or even extend it provided the provision is there. The Spring framework is like the basic structure of the house. It is a Java-based solution based on which you can build enterprise applications for the web. Li ntawd, if you think in terms of building an enterprise application, you already have the structure and it is about building on the structure.








Spring framework and testing framework

This section will discuss how the Spring framework can help the testing framework.

This was discussed earlier in this article that the caij nplooj ntoos hlav helps executing the unit and integration testing in the testing frameworks.

Unit testing support

Unit testing is about ensuring that individual components of the code such as the methods, classes and the services are fulfilling what they are supposed to do individually. Tam sim no, when it is about testing individual components, it can be tested at any stage of application development. But there are certain limitations when it comes to testing objects that are dependent because to fulfill the dependency, certain conditions need to be fulfilled and they may not be available at that stage of application development. Piv txwv, User and UserGroup are two classes and the former is dependent on the latter. UserGroup is used to hit the database for performing the VRUD operations. Tam sim no, if the application development is at the primary stage, the testing may not be possible because the database environment is not available. So you need mock objects to do this. The Spring framework provides support for mocking of all classes that cannot be tested because of certain limitations.

Koj yuav tau xav nyeem ntawv – Nthuav cov khoom rau lub caij nplooj ntoos hlav moj khaum



Integration testing support

The main purpose of integration testing is to find out whether all the components of a software application can work with one another in ways that are expected of the components. Piv txwv, a web service may be exposed to mock external systems to find out whether it is accepting requests and returning responses.

Hais meej, Thauj tw kev koom ua ke los soj ntsuam is performed, multiple classes and objects will be tested in conjunction. The Spring framework helps by auto-populating the classes and objects thus saving a lot of time needed to manually populate the classes and objects.

Other than the above, Spring framework also provides annotations support. Spring annotations are in fact designed for testing support. The Spring framework provides a set of Spring-specific annotations in the org.springframework.test.annotation package which can be used for testing purposes especially if the application is being developed based on Java 5 and upwards. The main annotations available in the framework are the following:

  • @Repeat
  • @Timed
  • @NotTransactional
  • @ExpectedException
  • @IfProfileValue
  • @ProfileValueSourceConfiguration

Writing sample application:

Integrate Spring with JUnit. Here you need to have spring-test.jar. First we are defining dependencies in pom.xml file.

Qhia 1: Defining dependency in pom.xml as described below

<quav dej caws>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<Version>4.11</Version>

<tau>ntsuam xyuas</tau>

<exclusions>

<exclusion>

<groupId>org.hamcrest</groupId>

<artifactId>hamcrest-core</artifactId>

</exclusion>

</exclusions>

</quav dej caws>

<quav dej caws>

<groupId>org.hamcrest</groupId>

<artifactId>hamcrest-library</artifactId>

<Version>1.3</Version>

<tau>ntsuam xyuas</tau>

</quav dej caws>

<quav dej caws>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<Version>4.3.0.TSO TAWM</Version>

</quav dej caws>

<quav dej caws>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<Version>4.3.0.TSO TAWM</Version>

<tau>ntsuam xyuas</tau>

</quav dej caws>








Following is the interface for validating assertions. Later we have implemented it in a class.

Listing2: SprTestService.java (Interface)

package com.techalpine.examples.spring;

public interface SprTestService {

boolean isValid(hlua);

}

Following is the implementation of the above interface. It overrides the isValid method.

Listing3: SprTestServiceImpl.java (Interface Implementation)

package com.techalpine.examples.spring;

import org.springframework.stereotype.Service;

@Service(“sprtstsrv”)

public class SprTestServiceImpl implements SprTestService {

@Override

public boolean isValid(hlua ) {

rov qab muaj tseeb;

}

}

This is the config class for component scanning.

Qhia 4: AppConfig.java (Lub caij nplooj ntoos hlav configuration)

package com.techalpine.examples.spring;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan(basePackages = {“com.techalpine.examples.spring”})

public class AppConfig {

}

Following is the main program to test the output. It checks the assertions and shows the output.

Qhia 5: SpringFrmWrkTest.java

pob com.techalpine.spring;

import com.techalpine.examples.spring.AppConfig;

import com.techalpine.examples.spring.SprTestService;

import com.techalpine.examples.spring.SprTestServiceImpl;

ntshuam org.junit.Test;

import org.junit.runner.RunWith;

import.springframework.taum... annotation.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.instanceOf;

import static org.hamcrest.Matchers.is;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = {AppConfig.class})

public class SpringFrmWrkTest {

//Quav tshuaj txhaj

@Autowired

@Qualifier(“sprtstsrv”)

SprTestService sprtstsrv;

@Test

public void spr_test_sprtstsrv_return_true() {

//Assert for correct impl

assertThat(sprtstsrv, instanceOf(SprTestServiceImpl.class));

//Assert for true value

assertThat(sprtstsrv.isValid(“”), yog(tseeb));

}

}

If we run the above application, it will show the output as TRUE.

Yuav tsum nyeem – Articles on testing domain








Xaus

Automation testing is seen as an important component of testing discipline these days by the enterprises because they believe it saves time and resource. Txawm li cas los, fulfillment of automation testing framework is not possible unless the support is provided. Piv txwv, without mocking support, automated unit testing would be incomplete and enterprises would face the possibility of erroneous software at the time when they should be approaching the last lap of testing. Spring framework automates a lot of things and the great thing about it is that enterprises do not need to reinvent anything given there. It is about using and reusing only.

Kauj ruam los mus teem lub sij hawm pab raws qib kev siv moj khaum caij nplooj ntoos hlav

Tsev Restful web kev siv caij nplooj ntoos hlav?

Kev siv siv lub caij nplooj hlav moj khaum AOP

Kauj ruam mus integrate rau lub caij nplooj hlav & Java lus Service

Lub caij nplooj hlav rau Apache Hadoop yog dab tsi?

 

 

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share