problem
In Java, JUnit is generally used as the unit test framework, and the objects to be tested are typically service and DAO, or Remoteservice and controller. All of these test objects are basically spring-managed and are not directly new. Each TestCase class is created by JUnit. How to inject these dependencies into each testcase instance. expected effect
We
Recently, at the request of the boss, study the spring test related things, and strive to make a convenient use of testing tools, for a spring is not familiar to the JUnit people this is a big pit, swept aside spring test document did not feel any gain, What spring does is j
@BeforeClass acting on a classExecutes once for all methods in a classproperty to be declared as static.@Before Acting on methodsEvery test method is executed once@Test Join the testJUnit order problem, performed alphabetically. To execute in an orderly manner, you can use testng,testng to add dependentJUnit Union Spring@RunWith (Springjunit4classrunner. Class) //Spring and
Direct use of JUnit to test the shortage of Spring programs
you need to manually fetch the bean using hard coding: in the test case, we need to get the target Bean to be tested from the SPIRNG container by means of the Applicationcontext.getbean () method, and also to do the modeling operation.
The database site is vulnerable to damage: The test method may make changes to the database records,
"Spring" JUnit loading spring containers for unit testingRead Catalogue
> Basic Construction
> Common usage
If we need to do unit testing of our service methods, and just like spring as an IOC container, we can configure JUnit to load the
When you write JUnit single measurements using spring, you can use the annotations to start and configure a jar package for spring Pom to introduce the
testing base Classes and JUnit tests
Package com.simple.test;
Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Imp
Spring Boot Junit unit test
The old Junit technology is nothing more than to mention, to some extent, to illustrate its importance in the project.Based on my own feelings and experience, the Junit Use Cases Written completely in the project to cover most of the Business Code should not exceed half.
I wrote some posts a
If we need to do unit testing of our service methods, and just like spring as an IOC container, we can configure JUnit to load the spring container so that it is easy to do unit testing.> Basic Construction(1) Introduce the required package(2) Write test classIn the test class you want to set which spring configuration
1. To simplify JUnit testing, the spring framework can also be used to integrate testing2. Specific stepsRequirement: There must be a junit environment first (that is, a development environment that has been imported into the JUNIT4)Step one: Introduce Spring-test.jar in the programStep Two: Add annotations on the spec
Create a Web projectConfiguring the Pom.xml file------equivalent to a jar packageConfigure APPLICATION.YML-----Profile (Spring database connection, Server service, logging log, etc.)Create package (entity, Controller, service, configuration, repository, interceptor, etc.)Small example:Entity:personData Persistence (DAO):ControllerSpring comes with JUnit test @springboottest without having to configure
Packagecn.mf.b_test;ImportJavax.annotation.Resource;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCn.mf.bean.User;//help us create a container@RunWith (Springjunit4classrunner.class)//specify which configuration file to use when creating the container@ContextConfiguration ("Classpath:applicationContext.xml") Public classDemo {//injecting an object named us
In the "programmers who don't write unit tests are not good programmers" principle, I'm sticking with the unit test, not saying that all Java Web applications are based on spring, but at least more than half are spring-based. After discovering bean management through spring, there are a variety of deficiencies in testingFor example, when many people do unit tests
.domainClass Name: Teacher.javaContents of the class:@Component ("Teacher")public class Teacher {public void SayHello () {System.out.println ("Hello");}}2. Configuring annotation scanning and annotation parsing in the configuration fileV. Testing1. Create the test catalog in the project/test2. Creating a test package under the test directoryPackage Name: Cn.jbit.spring100806.domain3. Create a test class under a packageClass Name: Teachertest.javaContent:@RunWith (Springjunit4classrunner.class)@C
Spring and junit testing and Springjunit Testing
1. We know that both J2SE and android have junit tests. Using junit can help you easily test the code. In my previous blog, I also wrote some junit test examples for J2SE. Today I will give a little talk about
With the principle that "programmers who don't write unit tests are not good programmers," I'm sticking to the unit test, not saying that all Java Web Apps are based on spring, but at least more than half are based on spring. It is found that after bean management through spring, there are a variety of deficiencies in testing,
For example, when a lot of people do
In the "programmers who don't write unit tests are not good programmers" principle, I'm sticking with the unit test, not saying that all Java Web applications are based on spring, but at least more than half are Spring-based. After discovering bean management through spring, there are a variety of shortcomings in testing, for example, when many people do unit tes
the entire class is ignored if it is used to decorate the class@Ignore ("Not Ready Yet")@Test@RunWithThere are many runner in JUnit, they are responsible for invoking your test code, each runner has its own special features, and you need to choose different runner to run your test code as needed.If we are simply doing plain Java testing and do not involve the Spring Web project, you can omit the @runwith a
The content of this section:
The meaning of spring's integration with JUnit testing
Spring consolidates JUnit tests
The meaning of spring and integrated JUnit testingPrior to the integration of JUnit, we wrote th
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.