1 /* 2 * Copyright 2023 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example; 18 19 import com.code_intelligence.jazzer.junit.FuzzTest; 20 import org.junit.jupiter.api.Assertions; 21 import org.junit.jupiter.api.MethodOrderer; 22 import org.junit.jupiter.api.TestMethodOrder; 23 import org.junit.jupiter.api.extension.ExtendWith; 24 import org.junit.jupiter.api.extension.ExtensionContext; 25 import org.junit.jupiter.api.extension.TestInstancePostProcessor; 26 27 @TestMethodOrder(MethodOrderer.MethodName.class) 28 @ExtendWith(AutofuzzLifecycleFuzzTest.AutofuzzLifecycleInstancePostProcessor.class) 29 class AutofuzzLifecycleFuzzTest { 30 // Use a TestInstancePostProcessor to inject an object into the JUnit test instance, 31 // simulating other JUnit extensions like the Spring Boot Test, to check that autofuzz 32 // invokes the test function on the correct instance. 33 private Object injectedObject; 34 35 @FuzzTest(maxDuration = "1s") autofuzzLifecycleFuzz(String ignored, String ignoredAsWell)36 void autofuzzLifecycleFuzz(String ignored, String ignoredAsWell) { 37 Assertions.assertNotNull(injectedObject); 38 } 39 40 static class AutofuzzLifecycleInstancePostProcessor implements TestInstancePostProcessor { 41 @Override postProcessTestInstance(Object o, ExtensionContext extensionContext)42 public void postProcessTestInstance(Object o, ExtensionContext extensionContext) { 43 ((AutofuzzLifecycleFuzzTest) o).injectedObject = new Object(); 44 } 45 } 46 } 47