0

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?

Greta
  • 300
  • 1
  • 10
  • Seems to me that the test case itself is OK. Should be interesting to know the response status (which is _not_ 200.) And perhaps to see the endpoint under test. – jensgram Feb 24 '21 at 12:05
  • Depends upon what the controller methods for those endpoint actually do and how/where your test data comes from(if applicable). When you test `getLogo` where does it get the logo from? does the `deleteLogo` delete all the logo in your persistence layer? that could explain why `getLogo` fails if `deleteLogo` has run before it. – madteapot Feb 24 '21 at 12:09
  • @jensgram I get an Exception, this is why the test fails. I added further information to the main question. Thank you. – Greta Feb 24 '21 at 12:09
  • @Setu If I ommit the deleteLogo()-Test the exception remains the same. The getLogo()-method gets the logo from the resources folder and I can also see that the image is there. – Greta Feb 24 '21 at 12:10
  • For `NullPointerException` simply put breakpoints before the line where it is throwing the exception and debug to find out which variable/method is returning null. Then debug the `getLogo` test alone and see what value the null variable/method is returning. https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – madteapot Feb 24 '21 at 12:16
  • @Setu 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? – Greta Feb 24 '21 at 12:44

0 Answers0