在我的junit测试中服务被设置为null

z4iuyo4d  于 2021-07-16  发布在  Java
关注(0)|答案(3)|浏览(366)

我正在为我的springboot应用程序创建测试。应用程序通过get调用,在requestbody中传递我的“carrequest”

public class CarsRequest implements Serializable {
    private String name;
    private String plate ;
    private  String price;
}

它还给了我与数据相关的汽车规格

{
   "name":"",
   "plate":"",
   "price":"",
   "brand":"",
   "kilometers":"",
   "revisiondate":"",
   "owner":""
}

我用mockito做了这个简单的测试,但我不明白为什么我的服务默认设置为null,这会抛出nullpointerexception中的所有内容

public class CarTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private CarService service;
    @Autowired
    ObjectMapper objectMapper;
    @Test
    public void TestOk() throws Exception{
        CarsRequest carsRequest = new CarsRequest();
        Car  car = new Car();
        List<Car> cars = new ArrayList<>();
        //septum the fields of cars and add them to the list
        cars.add(car);
        Mockito.when(
                service.getByPlate("bmw",
                        "TG23IO", "1500")).thenReturn(cars);
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
                "/garage/cars").accept(
                MediaType.APPLICATION_JSON);
        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        System.out.println(result.getResponse());
        String expected = "{name:"bmw","plate":"TG23IO","price":"1500","brand":"POL","kilometers":"80000","revisiondate":"2016-03-15","owner":"JohnLocke"}";
        JSONAssert.assertEquals(expected, result.getResponse()
                .getContentAsString(), false);
    }
}

下面我还添加了我的carservice

@Service
public class CarService {
    @Autowired
    CarRepository carRepository;
    @Autowired
    ObjectMapper objectMapper;
    public List<Cars> getByContratto(String name, String plate, String price) throws JsonProcessingException {
        //some code, extraction logic
        return cars;
    }
}

应用程序运行得很好,只是测试不起作用。作为一个测试写作的新手,我不知道我的carservice上的null是由于什么。如果需要的话,我也可以包括controller get和repository,但我认为它们没有帮助

qco9c6ql

qco9c6ql1#

我相信你在测试控制器。因此,在测试类的顶部包含以下注解。

@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)

另外,我可以看到在测试代码中carservice被引用,而共享的服务代码包含documentservice。

xoshrz7s

xoshrz7s2#

尝试添加 @RunWith(SpringRunner.class) 去你的测试班。
我怀疑你在使用 Junit 4 为了你的测试。在 Junit 4 你需要加上 @RunWith(SpringRunner.class) 在测试中,否则将忽略所有注解。更多信息请查看这里的文档。 46.3 Testing Spring Boot Applications 是回答您问题的部分。
不过,我建议您迁移到 Junit 5 如果可能的话。

@RunWith(SpringRunner.class)
public class CarTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private CarService service;
    @Autowired
    ObjectMapper objectMapper;
    @Test
    public void TestOk() throws Exception{
        // code here
        JSONAssert.assertEquals(expected, result.getResponse()
                .getContentAsString(), false);
    }
}
j8yoct9x

j8yoct9x3#

假设您只想测试您的控制器层(因为您使用的是mockmvc)并且您使用的是junit4(从您的代码中可以看到),那么您应该编写如下的测试用例

@RunWith(SpringRunner.class)
@WebMvcTest(SomeController.class)
public class CarTest {
    @MockBean
    private SomeService service;

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldPassTest() {
        BDDAssertions.assertThat(service).isNotNull();
    }
}

注意:在这个例子中,测试方法是不相关的,我添加这个只是为了说明。
如果有帮助,请告诉我们。

相关问题