diff options
Diffstat (limited to 'examples/junit-spring-web/src/test')
9 files changed, 106 insertions, 0 deletions
diff --git a/examples/junit-spring-web/src/test/java/com/example/JunitSpringWebApplicationTests.java b/examples/junit-spring-web/src/test/java/com/example/JunitSpringWebApplicationTests.java new file mode 100644 index 00000000..2cf356f2 --- /dev/null +++ b/examples/junit-spring-web/src/test/java/com/example/JunitSpringWebApplicationTests.java @@ -0,0 +1,100 @@ +/* + * Copyright 2023 Code Intelligence GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; + +import com.code_intelligence.jazzer.api.FuzzedDataProvider; +import com.code_intelligence.jazzer.junit.FuzzTest; +import com.example.JunitSpringWebApplication.HelloRequest; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@WebMvcTest +public class JunitSpringWebApplicationTests { + private static final ObjectMapper mapper = new ObjectMapper(); + + @Autowired private MockMvc mockMvc; + private boolean beforeCalled = false; + + @BeforeEach + public void beforeEach() { + beforeCalled = true; + } + + @AfterEach + public void afterEach() { + beforeCalled = false; + } + + @Test + public void unitTestShouldPass() throws Exception { + mockMvc.perform(get("/hello").param("name", "Maven")); + } + + @Test + public void unitTestShouldFail() throws Exception { + mockMvc.perform(get("/buggy-hello").param("name", "error")); + } + + @FuzzTest(maxDuration = "10s") + public void fuzzTestShouldPass(FuzzedDataProvider data) throws Exception { + if (!beforeCalled) { + throw new RuntimeException("BeforeEach was not called"); + } + + String name = data.consumeRemainingAsString(); + mockMvc.perform(get("/hello").param("name", name)); + } + + @FuzzTest(maxDuration = "10s") + public void fuzzTestShouldFail(FuzzedDataProvider data) throws Exception { + if (!beforeCalled) { + throw new RuntimeException("BeforeEach was not called"); + } + + String name = data.consumeRemainingAsString(); + mockMvc.perform(get("/buggy-hello").param("name", name)) + .andExpect(content().string(containsString(name))); + } + + @FuzzTest(maxDuration = "10s") + public void fuzzTestWithDtoShouldFail(HelloRequest helloRequest) throws Exception { + if (!beforeCalled) { + throw new RuntimeException("BeforeEach was not called"); + } + Assumptions.assumeTrue( + helloRequest != null && helloRequest.name != null && !helloRequest.name.isBlank()); + + mockMvc + .perform(post("/hello") + .contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(helloRequest))) + .andExpect(content().string(containsString(helloRequest.name))); + } +} diff --git a/examples/junit-spring-web/src/test/resources/application.properties b/examples/junit-spring-web/src/test/resources/application.properties new file mode 100644 index 00000000..64c19670 --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework.web=INFO diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/Test-001 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/Test-001 new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/Test-001 @@ -0,0 +1 @@ +error
\ No newline at end of file diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 @@ -0,0 +1 @@ +error
\ No newline at end of file diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 Binary files differnew file mode 100644 index 00000000..58002425 --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/Test-001 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/Test-001 new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/Test-001 @@ -0,0 +1 @@ +error
\ No newline at end of file diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 new file mode 100644 index 00000000..760589cb --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-11f9578d05e6f7bb58a3cdd00107e9f4e3882671 @@ -0,0 +1 @@ +error
\ No newline at end of file diff --git a/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 Binary files differnew file mode 100644 index 00000000..58002425 --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/com/example/JunitSpringWebApplicationTestsInputs/fuzzTestWithDtoShouldFail/crash-4acd17b34d3dafa673ab1f7ade3a8a29582a5730 diff --git a/examples/junit-spring-web/src/test/resources/junit-platform.properties b/examples/junit-spring-web/src/test/resources/junit-platform.properties new file mode 100644 index 00000000..02290619 --- /dev/null +++ b/examples/junit-spring-web/src/test/resources/junit-platform.properties @@ -0,0 +1 @@ +jazzer.instrument=com.example.**,com.other.package.** |