spring-test

Inject @AuthenticationPrincipal when unit testing a Spring REST controller

我们两清 提交于 2019-12-04 00:02:39
问题 I am having trouble trying to test a REST endpoint that receives an UserDetails as a parameter annotated with @AuthenticationPrincipal. It seems like the user instance created in the test scenario is not being used, but an attempt to instantiate using default constructor is made instead: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.andrucz.app.AppUserDetails]: No default constructor found; REST endpoint: @RestController @RequestMapping("/api/items") class

Maven: NoClassDefFoundError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner

岁酱吖の 提交于 2019-12-03 23:39:36
问题 I have a classpath issue. Background: Building and running a Spring WebApp. Originally it was one big project including dao/service/controller/webapp. I have just broken my project into a maven module project essentially splitting the webapp from the dao and service layers. Now my webapp junit tests do not run. The junit code has not changed and I don't think the dependencies have changed (albeit shifted around). Spring-test is in my local repository. Spring-test is in my unit test runtime

How configure correctly @RunWith(Parameterized.class) + SpringClassRule + SpringMethodRule with a custom @Rule?

自闭症网瘾萝莉.ら 提交于 2019-12-03 20:58:45
I am working with Spring Framework 4.3.x and JUnit 4, I have the following structure @Transactional @WebAppConfiguration @RunWith(Parameterized.class) @ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class}) @TestExecutionListeners(listeners={LoggingTestExecutionListener.class}, mergeMode=MergeMode.MERGE_WITH_DEFAULTS) public class CompleteTest { @ClassRule public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule(); @Rule public final SpringMethodRule springMethodRule = new SpringMethodRule(); Thus the combination of: @RunWith

Use one spring boot context through all SpringBootTests

此生再无相见时 提交于 2019-12-03 16:28:02
I want to be able to cache application context through different classes with tests using junit. Test classes are declared this way: @SpringBootTest @RunWith(SpringRunner.class) public class SomeIntegrationTest { } I saw this question Reuse spring application context across junit test classes but in this case I don't use any xml and I want to start context completely, not just few beans from it, so @SpringBootTest is more suitable than @ContextConfiguration , if I got it right. Andres Cespedes Morales Ruslan, so your question is on how to reuse the Spring Boot Context for a JUnit Suite, right?

spring-boot testing - Could multiple test share a single context?

五迷三道 提交于 2019-12-03 16:13:28
问题 I created multiple spring-boot testing class, (with spring-boot 1.4.0 ) . FirstActionTest.java : @RunWith(SpringRunner.class) @WebMvcTest(FirstAction.class) @TestPropertySource("classpath:test-application.properties") public class FirstActionTest { @Autowired private MockMvc mvc; // ... } SecondActionTest.java : @RunWith(SpringRunner.class) @WebMvcTest(SecondAction.class) @TestPropertySource("classpath:test-application.properties") public class SecondActionTest { @Autowired private MockMvc

Writing tests to verify received msg in jms listener (Spring-Boot)

人走茶凉 提交于 2019-12-03 15:07:03
I want to write test for something like below; There is a listener called state-info-1 in src/main . It does some changes to any message it gets and publishes the new message on activemq topic state-info-2 . I will build a dummy message and publish on to activemq topic state-info-1 . Finally verify that, the received message on topic state-info-2 is like i expected. My Listeners are like; @JmsListener(destination = "state-info-1", containerFactory = "connFactory") public void receiveMessage(Message payload) { // Do Stuff and Publish to state-info-2 } Is it possible i can write test for this?

Testing Spring asyncResult() and jsonPath() together

元气小坏坏 提交于 2019-12-03 11:15:11
I'm using a restful url to kick off a long-running backend process (it is normally on a cron schedule, but we want the ability to kick it off manually). The code below works and I see the result in the browser when I test manually. @ResponseBody @RequestMapping(value = "/trigger/{jobName}", method = RequestMethod.GET) public Callable<TriggerResult> triggerJob(@PathVariable final String jobName) { return new Callable<TriggerResult>() { @Override public TriggerResult call() throws Exception { // Code goes here to locate relevant job and kick it off, waiting for result String message = <result

How to test POST spring mvc

扶醉桌前 提交于 2019-12-03 08:48:49
My problem is to how to call this. I could do MyObject o = new MyObject(); myController.save(o, "value"); but this is not what I would like to do. I would like the MyObject to be in the request post body? How can this be done? @Requestmapping(value="/save/{value}", method=RequestMethod.POST) public void post(@Valid MyObject o, @PathVariable String value{ objectService.save(o); } Just to be clear I am talking about unit testing. Edit: @RequestMapping(value = "/", method = RequestMethod.POST) public View postUser(ModelMap data, @Valid Profile profile, BindingResult bindingResult) { if

How to autowire field in static @BeforeClass?

做~自己de王妃 提交于 2019-12-03 06:23:30
问题 @RunWith(SpringJUnit4ClassRunner.class) public void ITest { @Autowired private EntityRepository dao; @BeforeClass public static void init() { dao.save(initialEntity); //not possible as field is not static } } How can I have my service injected already in the static init class? 回答1: It looks to me that you are trying to populate DB before tests. I would give a try to two options: If you can extract initial scripts to sql file (if that is option for you without using repository bean) you can

Use different Spring test context configuration for different test methods

試著忘記壹切 提交于 2019-12-03 06:12:30
We have a Spring based JUnit test class which is utilizing an inner test context configuration class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ServiceTest.Config.class) public class ServiceTest { @Test public void someTest() { ... @Configuration @PropertySource(value = { "classpath:application.properties" }) @ComponentScan({ "..." }) public static class Config { ... New functionalities have been recently introduced to the Service class, for which the concerned tests should be added to ServiceTest. However these would also require a different test context