Last active
September 9, 2020 21:12
-
-
Save ChrisLeNeve/c486760f4c292e532ef517fb7add8d3e to your computer and use it in GitHub Desktop.
Test cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@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