Skip to content

Instantly share code, notes, and snippets.

@ChrisLeNeve
Last active September 9, 2020 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisLeNeve/c486760f4c292e532ef517fb7add8d3e to your computer and use it in GitHub Desktop.
Save ChrisLeNeve/c486760f4c292e532ef517fb7add8d3e to your computer and use it in GitHub Desktop.
Test cheat sheet
//@RunWith(SpringJUnit4ClassRunner.class) // for JUnit 4
//@ExtendWith(MockitoExtension.class) // for running with a Mockito runner. No Spring features
@ExtendWith(SpringExtension.class) // for running with a Spring runner. Loads Spring features (bean loading, etc)
@DisplayName("My test class name (JUnit 5)")
//@SpringBootTest // will load application context
public class MyTest {
// Strategy for loading one specific bean
// If not in a static inner class: needs to be imported on class with @Import
@TestConfiguration
static class NeededBeanConfig {
@MockBean
private OtherBean necessaryOtherBean;
@Bean
public NeededBean neededBean() {
return new NeededBean(necessaryOtherBean);
}
}
private BeanToMock beanToMock; // beans to be mocked
@InjectMocks
private SystemUnderTest systemUnderTest;
@Autowired
private NeededBean actualBean;
@BeforeEach
public void setup() {
beanToMock = Mockito.mock(BeanToMock.class);
systemUnderTest = new SystemUnderTest(beanToMock, actualBean);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment