I have a test class where all tests pass when I run them all at once except from getLogo(). The test getLogo() only passes when I run it individually and I have not the slightest clue why.
This is my test class:
@WebAppConfiguration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.example")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ReceiptApplication.class})
public class PropertiesServiceTest {
@Autowired
private ConfigPropertiesService configPropertiesService;
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void uploadLogo() throws Exception {
MockMultipartFile file = new MockMultipartFile("logo", "originalMock.jpg", null, "bar".getBytes());
mockMvc
.perform(multipart("/settings/uploadLogo").file(file))
.andExpect(status().isOk());
}
@Test
public void getLogo() throws Exception {
mockMvc
.perform(get("/settings/getLogo"))
.andExpect(status().is(200));
}
@Test
public void deleteLogo() throws Exception {
mockMvc
.perform(delete("/settings/deleteLogo"))
.andExpect(status().is(200));
}
}
The Exception I get is
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
And the endpoint /getLogo looks like so:
@GetMapping(value = "/getLogo")
public String getLogo() {
return new Gson().toJson(configPropertiesService.getLogo());
}
The getLogo-method in configPropertiesService:
public byte[] getLogo() {
ConfigPropertiesEntity configPropertiesEntity = configPropertiesRepository.findByName("com.example.logoName");
if (configPropertiesEntity != null) {
try {
InputStream in = getClass().getResourceAsStream("/static/photos/" + configPropertiesEntity.getValue());
return IOUtils.toByteArray(in);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
I know why I get a NullPointerException, its because the file is not found when the test is run together with the others, the application runs into the catch block in this case. But why does this not happen when I run the test individually? How can I change the test so that they also pass when I run them together?