After PS: idea is opened, it is found that the test class reports an error: cannot resolve symbol ‘spring junit4classrunner’, and the annotations are all red
package com.it;
import com.it.config.SpringConfiguration;
import com.it.entity.Student;
import com.it.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* **
* * Using Junit unit tests: testing our configuration
* * Spring integration junit configuration
* * 1, import spring integration junit jar: spring-test (coordinates)
* * 2, using a note provided by Junit to replace the original main method, replaced with spring provided
* * @Runwith
* 3. tell the spring runner that spring and ioc creation is based on xml or annotations, and the location
* * @ContextConfiguration
* * locations: specifies the location of the xml file, plus the classpath keyword, indicating that it is under the class path
* * classes: specifies the location of the annotated class
* *
* * When we use spring 5.x version, we require the junit jar to be 4.12 and above
* */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class TestClient {
@Autowired
private StudentService as;
@Test
public void findAll(){
List<Student> studentList =as.findAllStudent();
for (Student student:studentList) {
System.out.println(student);
}
}
@Test
public void findbyidtest() {
//ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student student = as.findByid(4);
System.out.println(student);
}
@Test
public void saveTest() {
Student student=new Student();
student.setId(7);
student.setStuno("10007");
student.setName("chen");
student.setClassid(2);
as.saveStudent(student);
}
@Test
public void updatetest() {
Student student = as.findByid(4);
student.setName("liu");
as.updateStudent(student);
}
@Test
public void deletetest() {
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService as = ac.getBean("studentService", StudentService.class);
as.deleteStudent(7);
}
}
Remove & lt; scope> test</ scope>, After removing it, it can run successfully, but what’s the reason for this??Let’s first take a look at POM dependency & lt; scope>????& lt;/ scope> What does it mean
In a maven project, if there are jar packages that need to be compiled but not published, you can use the scope tag and set the value to provided. As follows:
< dependency>
< groupId> javax.servlet.jsp</ groupId>
< artifactId> jsp-api</ artifactId>
< version> 2.1</ version>
< scope> provided</ scope>
< classifier />
</ dependency>
Other parameters of scope are as follows:
compile
the default scope indicates that dependency can be used in the life cycle. Moreover, these dependencies are passed to the dependent projects. Applicable to all phases and will be released with the project
provided
it is similar to compile, but it indicates that dependency is provided by JDK or container, such as servlet AP and some Java EE APIs. This scope can only be used for compilation and testing, and has no transitivity
runtime
indicates that dependency does not work at compile time, but it will work at run time and test time, such as jdbc driver, which is suitable for run time and test phase
test
indicates that dependency is used in the test and not in the runtime. Used only when testing, to compile and run test code. Will not be published with the project
system
it is similar to provided, but it should be provided in the form of external jar package in the system, and Maven will not find it in the repository
In fact, you are not familiar with the Maven file structure. Test classes are usually placed in Src/test/Java, not in Src/main/Java. When Maven is compiled, it is not referenced in Src/main/Java; scope> test</ scope> While compiling tests under Src/test/Java, this will refer to < scope> test</ scope> The reason may be that after replacing the original main method with @ runwith provided by spring with an annotation provided by JUnit, < scope>????< scope> At this time, there is no need to test the values in it. Test only works when testing, and those not testing will not work. If there are, springjunitclassrunner.class will not be found at this time
Similar Posts:
- Springboot: solve the problem of failed to load ApplicationContext
- junitjava.lang.Exception: No tests found matching
- Solve the error of springboot failed to load ApplicationContext
- [Solved] org.hibernate.MappingException: Unknown entity: com.huangliusong.entity.Person
- [How to Solve] RestController cannot be recognized in spring boot
- Mockito can’t solve the problem of mockfinal class
- Springboot always reports an error when importing JSP dependencies
- Unregistering JMX-exposed beans on shutdown Solution
- [Solved] java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
- [Solved] Caused by: org.xml.sax.SAXParseException: Premature end of file.