Create abstract class for unit test in spring boot without tomcat - spring

I try to create abstract unit test class in Spring Boot. While I can a unit test following:
#WebMvcTest(Rest.class)
class AbstractRestTest {
#Autowired
MockMvc mvc;
void baseTest(String urlTemplate, String expectedValue) throws Exception {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get(urlTemplate);
MvcResult results = mvc.perform(request).andReturn();
assertEquals(expectedValue, results.getResponse().getContentAsString());
}
#Test
public void test1() throws Exception {
baseTest(urlText, expected);
}
}
But while I create an abstract class following, don't run the test:
public class RestTests extends AbstractRestTest {
#Test
public void test2() throws Exception {
baseTest(urlText, expected);
}
}

Related

Spring Boot Test model attribute does not exist

Im trying to test a controller, Author Controller, which returns a view with a model. The problem is on the testInitUpdateAuthor() test where its not able to find the model or attribute name specifically. All other methods are fine with their model/attribute tests.
Any advice?
#Slf4j
#Controller
public class AuthorController {
private final AuthorService authorService;
private final String CREATEORUPDATEFORM = "author/createOrUpdateAuthor";
public AuthorController(AuthorService authorService) {
this.authorService = authorService;
}
#GetMapping("/author/{id}/update")
public String updateAuthor(#PathVariable("id") Long id, Model model) {
model.addAttribute("author", authorService.findById(id));
return CREATEORUPDATEFORM;
}
#ExtendWith(MockitoExtension.class)
#SpringBootTest
class AuthorControllerTest {
MockMvc mockMvc;
#Mock
AuthorService authorService;
#InjectMocks
AuthorController authorController;
#BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(authorController).build();
}
#Test
void getIndex() throws Exception {
mockMvc.perform(get("/author/index"))
.andExpect(status().isOk())
.andExpect(view().name("author/index"))
.andExpect(model().attributeExists("authors"));
}
#Test
void testInitUpdateAuthor() throws Exception {
when(authorService.findById(anyLong())).thenReturn(Author.builder().id(1L).build());
mockMvc.perform(get("/author/1/update"))
.andExpect(status().isOk())
.andExpect(view().name("author/createOrUpdateAuthor"))
.andExpect(model().attributeExists("author"));
}
}

Problems using dbunit with Spring (without spring-test-dbunit)

I'm trying to use dbunit to test my DAOs. We use Spring in a version that is not compatible with spring-test-dbunit. I can't autowire my dao beans into my test class, because then I would have to use #RunWith(SpringJUnit4ClassRunner.class) which regards one parameterless constructor. My class looks like following:
public class DbUnitExample extends DBTestCase {
#Autowired
public MyDAO myDAO;
public DbUnitExample(String name) {
super(name);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "...");
}
#Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new FileInputStream("target/partial.xml"));
}
#Override
protected DatabaseOperation getSetUpOperation() throws Exception {
return DatabaseOperation.REFRESH;
}
#Override
protected DatabaseOperation getTearDownOperation() throws Exception {
return DatabaseOperation.NONE;
}
#Test
public void testSometing() throws Exception {
myDAO.deleteById(12662);
}
}
Of course I get an NPE because my dao bean can't be found. When I use #RunWith(SpringJUnit4ClassRunner.class) I need to provide one parameterless constructor and have to delete my "dbunit"-constructor. Is there a standard way or workaround to use dbunit with spring without the use of spring-test-dbunit
EDIT
My class now looks like following:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("/test-application.xml")
#DirtiesContext
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
public class DbUnitExample extends DBTestCase {
#Autowired
public MyDAO myDAO;
public DbUnitExample() {
super("target/partial.xml");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "...");
}
#Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new FileInputStream("target/partial.xml"));
}
#Override
protected DatabaseOperation getSetUpOperation() throws Exception {
return DatabaseOperation.REFRESH;
}
#Override
protected DatabaseOperation getTearDownOperation() throws Exception {
// return DatabaseOperation.NONE;
// return DatabaseOperation.REFRESH;
return DatabaseOperation.CLEAN_INSERT;
}
#Test
public void testSometing() throws Exception {
myDAO.deleteById(12662);
}
}
It compiles now, but has no dbunt-functionality, which means if I delete a row it doesn't get restored to it's previous state (inserted again).
Since you are using Spring, I suggest autowiring the dbUnit instances into the test. The dbUnit Test Cases page has "Configuration Example Using Spring" for the PrepAndExpectedTestCase, but just copy the code and change it to DBTestCase and adjust accordingly.

Method when() from Mockito do not work correctly

I have problem with mockito method: when(...). When I test:
afterThrowExceptionShouldReturnCorrectHttpStatus()
Ran first, then the second test:
controllerShouldReturnListOfAnns()
It always fails because it throw NotFoundException. When I delete the first test or running second test as first, then everything is correct. This look like method when() from first test override method when() form the second test There is test code and test configuration.
#ActiveProfiles("dev")
#RunWith(SpringRunner.class)
#SpringBootTest
public class AnnTestController {
#Autowired
private AnnounService annSrv;
#Autowired
private AnnounRepo annRepo;
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Before
public void contextLoads() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
#Test
public void afterThrowExceptionShouldReturnCorrectHttpStatus() throws Exception {
when(this.annRepo.getAnnounList()).thenThrow(NotFoundAnnounException.class);
this.mockMvc.perform(get("/ann/list")).andExpect(status().isNotFound());
}
#Test
public void controllerShouldReturnListOfAnns() throws Exception {
List<Announcement> lst = new ArrayList<>();
lst.add(new Announcement(1, "test", "test"));
when(annRepo.getAnnounList()).thenReturn(lst);
this.mockMvc.perform(get("/ann/list"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is(1)));
}}
Config:
#Profile("dev")
#Configuration
public class BeanConfig {
#Bean
public CommentsRepo commentsRepo() {
return mock(CommentsRepo.class);
}}
You can try somthing like that:
#After public void reset_mocks() {
Mockito.reset(this.annRepo);
}

Spring integration test with PowerMockito + TestNG leads to InvocationTargetException

I'm trying to mock a static method with PowerMockito in an integration test with TestNG, but no joy so far.
#PrepareForTest({ HttpCommonClientUtils.class })
public class LiveChannelServiceTestNg extends LiveBaseTestNg {
#Autowired
private LiveChannelShareService liveChannelService;
#Resource(name = "liveSettingService")
private LiveSettingShareService liveSettingService;
#Autowired
private VCloudHelper vcloudHelper;
#ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
static String result = "{\\\"cid\\\":\\\"0aba025de6604ccb931bd9868bddba9a\\\",\\\"duration\\\":120,\\\"format\\\":0,\\\"gmtCreate\\\":1486378732486,\\\"hlsPullUrl\\\":\\\"http://pullhls03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a/playlist.m3u8\\\",\\\"httpPullUrl\\\":\\\"http://flv03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a.flv?netease=flv03f9fed7.live.126.net\\\",\\\"id\\\":1,\\\"name\\\":\\\"直播频道1\\\",\\\"needRecord\\\":1,\\\"pushUrl\\\":\\\"rtmp://p03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a?wsSecret=e5cf3b5c060271fd23ba56469a803a99&wsTime=1486378731\\\",\\\"referType\\\":0,\\\"rtmpPullUrl\\\":\\\"rtmp://v03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a\\\",\\\"status\\\":0,\\\"type\\\":0}";
static String cid = "0aba025de6604ccb931bd9868bddba9a";
#BeforeClass
public void setUp() throws Exception {
/* 公用的环境初始化... */
}
#BeforeMethod
public void testSetUp() throws Exception {
}
#Test
public void test_static() {
String result = "{\"cid\":\"0aba025de6604ccb931bd9868bddba9a\",\"duration\":120,\"format\":0,\"gmtCreate\":1486378732486,\"hlsPullUrl\":\"http://pullhls03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a/playlist.m3u8\",\"httpPullUrl\":\"http://flv03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a.flv?netease=flv03f9fed7.live.126.net\",\"id\":1,\"name\":\"直播频道1\",\"needRecord\":1,\"pushUrl\":\"rtmp://p03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a?wsSecret=e5cf3b5c060271fd23ba56469a803a99&wsTime=1486378731\",\"referType\":0,\"rtmpPullUrl\":\"rtmp://v03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a\",\"status\":0,\"type\":0}";
PowerMockito.mockStatic(HttpCommonClientUtils.class);
PowerMockito.when(HttpCommonClientUtils.getHtmlByPostMethod(Matchers.any(HttpClient.class),
Matchers.any(HttpDataProvider.class))).thenReturn(result);
LiveChannelDto dto = liveChannelService.getOrGenerateChannleByBindInfo(0, 1, 101L);
Assert.assertEquals(cid, dto.getCid());
}
When I run this test, a InvocationTargetException is appear

How to inject a bean in a ConstraintValidator with MockMVC?

I have a custom ConstraintValidator:
#Component
public class FooValidator implements ConstraintValidator<FooAnnotation, String> {
#Inject
private FooRepository fooRepository;
#Override
public void initialize(FooAnnotation foo) {
}
#Override
public boolean isValid(String code, ConstraintValidatorContext context) {
Foo foo = fooRepository.findByCode(code);
//My code
}
}
In my Junit tests and MockMVC I call the url but fooRepository bean validator is always null.
How I can inject it in my test controller? I tried to create a mock repository but it is also null.
My source code test:
public class FooControllerTest {
#InjectMocks
private FooController fooController;
private MockMvc mockMvc;
#Before
public void setup() {
// Process mock annotations
MockitoAnnotations.initMocks(this);
// Setup Spring test in standalone mode
this.mockMvc = MockMvcBuilders.standaloneSetup(fooController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build();
}
#Test
public void testSave() throws Exception {
Foo foo = new Foo();
// given
//My code...
// when
// then
// with errors
this.mockMvc.perform(post("/foo/update")
.param("name", "asdfasd")
.sessionAttr("foo", foo))
.andExpect(model().hasErrors())
.andExpect(model().attributeHasFieldErrors("foo", "name"));
}
}
You should add a #ContextConfiguration to your test, among other things.

Resources