spring-test

Spring boot test @Transactional not saving

社会主义新天地 提交于 2019-12-18 06:53:40
问题 I'am trying to do a simple Integration test using Spring Boot Test in order to test the e2e use case. My test does not work because I'am not able to make the repository saving data, I think I have a problem with spring contexts ... This is my Entity: @Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class Person { @Id private int id; private String name; } This is the Person repository: @Repository public interface PersonRepository extends JpaRepository<Person, Integer> {

Mockito Exception - when() requires an argument which has to be a method call on a mock

 ̄綄美尐妖づ 提交于 2019-12-18 03:01:41
问题 I have a very simple test case that is using Mockito and Spring Test framework. When I do when(pcUserService.read("1")).thenReturn(pcUser); I get this exception. org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods *cannot* be

Configure specific in memory database for testing purpose in Spring

旧巷老猫 提交于 2019-12-17 22:30:10
问题 How do I configure my Spring Boot application so that when I run unit tests it will use in-memory database such as H2/HSQL but when I run Spring Boot application it will use production database [Postgre/MySQL] ? 回答1: Spring profiles can be used for this. This would be a specific way: Have environment specific properties files: application.properties : spring.profiles.active: dev application-dev.properties spring.jpa.database: MYSQL spring.jpa.hibernate.ddl-auto: update spring.datasource.url:

Could not load TestContextBootstrapper - Spring Unit testing

半腔热情 提交于 2019-12-17 20:16:40
问题 I have to execute Unit test on one of my Dao classes using Spring. Here is my unit test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:app-config.xml"}) @ActiveProfiles("local") public class HouseDaoTest { @Autowired HouseDataDao houseDataDao; @Test public void saveTest(){ HouseData data = new HouseData(); Address add = new Address(); // Truncating for sake of simplicity houseDataDao.save(data); } } And My bean configuration files: app-config.xml: <?xml

Spring beans redefinition in unit test environment

别说谁变了你拦得住时间么 提交于 2019-12-17 17:24:45
问题 We are using Spring for my application purposes, and Spring Testing framework for unit tests. We have a small problem though: the application code loads a Spring application context from a list of locations (XML files) in the classpath. But when we run our unit tests, we want some of the Spring beans to be mocks instead of full-fledged implementation classes. Moreover, for some unit tests we want some beans to become mocks, while for other unit tests we want other beans to become mocks, as we

Reload or refresh a Spring application context inside a test method?

旧时模样 提交于 2019-12-17 16:11:24
问题 I need to change the Spring profiles that are active in my applicationContext within a single method of my test class, and to do so I need to run one line of code before refreshing the contest because I am using a ProfileResolver. I have tried the following: @WebAppConfiguration @ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"}) @ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class) public class ControllerTest extends AbstractTestNGSpringContextTests { @Test public void

request scoped beans in spring testing

跟風遠走 提交于 2019-12-17 07:14:21
问题 I would like to make use of request scoped beans in my app. I use JUnit4 for testing. If I try to create one in a test like this: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" }) public class TestScopedBeans { protected final static Logger logger = Logger .getLogger(TestScopedBeans.class); @Resource private Object tObj; @Test public void testBean() { logger.debug(tObj); } @Test public void testBean2() { logger.debug

Rollback transaction after @Test

混江龙づ霸主 提交于 2019-12-17 04:21:36
问题 First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question. I'm running JUnit tests using spring-test, my code looks like this @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {}) public class StudentSystemTest { @Autowired private StudentSystem studentSystem; @Before public void initTest() { // set up the database, create basic structure for testing } @Test public void test1()

Reuse spring application context across junit test classes

家住魔仙堡 提交于 2019-12-17 03:24:10
问题 We've a bunch of JUnit test cases (Integration tests) and they are logically grouped into different test classes. We are able to load Spring application context once per test class and re-use it for all test cases in a JUnit test class as mentioned in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html However, we were just wondering if there is a way to load Spring application context only once for a bunch of JUnit test classes. FWIW, we use Spring

Test Unit Spring boot: Unable to register mock bean

爱⌒轻易说出口 提交于 2019-12-14 03:06:07
问题 I have two classes of tests: 1- for the unit test of the Controller class and 2- for the unit test of the Service class as below: 1- Test class controller: @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class CentroDeCustoRestControllerTeste { @Autowired private MockMvc mvc; @MockBean private CentroDeCustoService service; @Test @WithMockUser(username = "teste-app@teste.com", authorities = {"ADMIN"}) public void