Tag Archives: error reporting nullpointer

[Solved] Unit test automatically injects error reporting nullpointer

Using @Autowired annotation to generate nullpointer in unit test

terms of settlement

1. Introduce POM

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.1.RELEASE</version>
            <scope>test</scope>
        </dependency>

2. Add notes to the unit test

@SpringBootTest(classes = EduApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class redisTemplate {

  @Autowired
  RedisTemplate redisTemplate;
    @Test
    public void testStringAdd(){
        BoundValueOperations str = redisTemplate.boundValueOps("str");
        // Set the value via redisTemplate
        str.set("test1");
        str.set("test2");
    }
}