check
| compiler.automake.allow.when.app.running |
2. Ctrl+Alt+S
Build, Execution, Deployment/Compiler
Check Build project automatically
Then press 'Apply'

Then press 'Apply'
| compiler.automake.allow.when.app.running |
@Service@Transactionalpublic class OrderServiceImpl implements OrderService { @Autowired private OrderRepository orderRepository; @Override @Transactional public Order save(Order order) throws ConstrainViolationException { Order saved; try { saved = orderRepository.save(order); } catch (ConstraintViolationException e) { throw new ConstrainViolationException(e.getMessage()); } return saved; }
@Test(expected = ConstrainViolationException.class) //@Transactionalpublic void whenSaveEmptyOrder_ThenReturnException() throws ConstrainViolationException { orderService.save(Order.builder() .searcher(new Searcher()) .lister(new Lister("Yichun", "Zhao")) .startTime(ZonedDateTime.now()) .endTime(ZonedDateTime.now().plusDays(100)) .build() );}
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE ) public class OrderServiceTest { @Autowired
ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.![]() |
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use
SpringApplication to create it. |
@SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication. In addition to@SpringBootTest a number of other annotations are also provided for testing more specific slices of an application.![]() |
If you are using JUnit 4, don’t forget to also add
@RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent @ExtendWith(SpringExtension) as @SpringBootTest and the other @…Test annotations are already annotated with it. |
@SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:MOCK(Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation. If a web environment is not available on your classpath, this mode transparently falls back to creating a regular non-web ApplicationContext. It can be used in conjunction with @AutoConfigureMockMvc or @AutoConfigureWebTestClient for mock-based testing of your web application.RANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a random port.DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a defined port (from your application.properties) or on the default port of 8080.NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment (mock or otherwise).![]() |
If your test is
@Transactional, it rolls back the transaction at the end of each test method by default. However, as using this arrangement with either RANDOM_PORT or DEFINED_PORT implicitly provides a real servlet environment, the HTTP client and server run in separate threads and, thus, in separate transactions. Any transaction initiated on the server does not roll back in this case. |
![]() |
@SpringBootTest with webEnvironment = WebEnvironment.RANDOM_PORT will also start the management server on a separate random port if your application uses a different port for the management server. |
@Embedded@Validprotected LoginInfo loginInfo;
@NotNull@Column(name = "PASS_WORD") protected String password;
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...