Springboot Test case failure - java

I have created JUnit cases for my springboot application, when I try to run the test cases, its always failing, it is giving error ApplicationTest.contextLoads:41 Expecting actual not to be null
Here is my Test Class
//#RunWith(SpringRunner.class)
#SpringBootTest(classes = ServiceApplication.class)
#ActiveProfiles({ Profiles.TEST })
#GraphQLTest
public class ApplicationTest {
#Autowired
TestController controller;
#Test
public void contextLoads() {
then(controller).isNotNull();
}
}
when running the application it works fine but test cases are failing. looks like the controller which I am annotating is not instantiating.
Any help would be apricated.

Related

Springboot REST API Testcase passing in local but failing in build

I have a test case class something like
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
#AutoConfigureWebTestClient
public class sampleTests {
when(test.getUrl()).thenReturn("http://localhost:8080/sampletest");
#Test
public void getEndpoint() throws Exception {
webTestClient.get().uri("/sampletest").exchange().expectStatus().isOk().expectBody().consumeWith(System.out::println).json(expectedBody);
}
}
This is passing in local, but got failed with the following error when executed in the azure DevOps pipeline.
java.lang.AssertionError: Status expected:<200 OK> but was:<500 INTERNAL_SERVER_ERROR>
Any insights on the same will be much appreciated.

Spring Boot #RestController #Autowired null in Unit tests

I can't understand why #Autowiring my #RestController class is returning null.
I want to do a basic unit test before doing an integrated test but its failing.
In fact anything that is being #Autowired is showing null in the test package.
I have a very simple test, I just want to see the basic works:
A very simple example:
#Component
public class SimpleComponent {
public int add(int a, int b){
return a + b;
}
}
And the test:
class SimpleComponentTest {
#Autowired
private SimpleComponent component;
#Test
public void givenSimpleComponentShouldNotBeNull(){
assertNotNull(component);//How on earth does this fail?
}
}
Here is the code from the Controller class:
#RestController
public class EmployeeAccountApi {
#PostMapping("/register")
public String register(){
return "Registering!";
}
}
public class EmployeeAccountApiUnitTest {
#Autowired
private EmployeeAccountApi employeeAccountApi;
#Test
public void testAutowiring(){
assertNotNull(employeeAccountApi);//Fails, Can't understand why
}
}
This works with the #Mock annotation:
But I dont want to mock it as that is the class I am unit testing.
I want to test that the method returns a basic string.
Question is why isn't it working?
#ExtendWith(MockitoExtension.class)
public class EmployeeAccountApiUnitTest {
#Mock
private EmployeeAccountApi employeeAccountApi;
#Test
public void testAutowiring(){
assertNotNull(employeeAccountApi);//This works
}
}
Even if I get the Employee Service that clearly worked and tested in the previous test also is null in this class:
public class EmployeeAccountApiUnitTest {
#Autowired
private EmployeeAccountApi employeeAccountApi;
#Autowired
private EmployeeService service;
#Test
public void testAutowiring(){
//assertNotNull(employeeAccountApi);
assertNotNull(service);
}
}
Why is #Autowired null in the Test?
Mocking is working fine
Plenty of similar questions but they are too specific and not provide any basic information
#Autowired only works if you are spinning up the application context for your application. Annotate the test class with #SpringBootTest, which tells the test to start the entire spring application context, and it should work.
#SpringBootTest has to be used on the test class for Spring to load your ApplicationContext and wire things up. Without out this there's nothing for Spring to inject.
You can use #WebMvcTest on the test class to focus your tests only on the Web Layer.
There's a whole bunch of annotations that allow you to "slice" your ApplicationContext up lots of other ways for testing various other parts of your application.
Spring's documentation and tutorials are really very good. You should check them out.
Testing the Web Layer

Is it possible to use Spring Retry with TestNg Spring context

I have TestNg tests and use AbstractTestNGSpringContextTests:
#ContextConfiguration(classes = ContextConfiguration.class)
public class BaseTest extends AbstractTestNGSpringContextTests {
.......
}
And enabled retry in my config class:
#Configuration
#EnableRetry
public class TestContextConfiguration {
...
}
I would like to retry send request method in my service client:
#Service
public class ApiClient {
#Retryable
public Response getUser(String name){
...
return response;
But it does not work, no retry happens when the method throwing exception.
At the same time it works with jUnit spring test runner. Also Spring AOP works properly with AbstractTestNGSpringContextTests.
What could be the problem?

#MockBean injected correctly but with null value on test class

I am using tests with Cucumber and Spring Boot
#CucumberContextConfiguration
#ActiveProfiles(profiles = { "cucumber" })
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
#ExtendWith(SpringExtension.class)
public class FooTest {
#Autowired
BatchService batchService;
#MockBean
S3ClientService s3ClientService;
#MockBean
HttpClientService httpClientService;
#SpyBean
UndueService undueService;
#Given("^foo cucumber test$")
public void foo_cucumber_test() {
System.out.println("Foo Test");
}
}
When I run/debug my test with a break point on #Given method
I got this weird behavior, the #Mockbean/#SpyBean are correctly injected but in the test class its values are null !! and I can't run Mockito functions verify or when
But when I run a test without cucumber
#Test
void fooTest() {
System.out.println("Foo Test");
}
It works fine !! the values are not null
So, mock beans are created but not injected to the test suite. I guess you should put both #Autowired and #MockBean annotations.
Here is a similar question: Spring #MockBean not injected with Cucumber

Mockito with jUnit ParallelComputer

I am writing integration test for a spring boot controller for concurrent access of APIs. I want to run these tests in parallel using a common setup. I looked at jUnit ParallelComputer but couldn't find a way to create a common setup. Even if I find a way to do the common setup the #Autowired variables are not getting instantiated, they remain null. Am I missing something?
Here is an example of what I am doing:
#RunWith(SpringJUnit4ClassRunner.class)
public class ParallelComputerTest {
#Test
public void test() {
Class[] cls={ParallelTest1.class};
//Parallel all methods in all classes
JUnitCore.runClasses(new ParallelComputer(true, true), cls);
}
}
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
#AutoConfigureMockMvc
public class ParallelTest1 {
#Autowired
private MockMvc mockMvc;
#Autowired
private someClass;
#Test public void a(){
//someClass and mockMvc is null here
//do something
}
#Test public void b(){}
}
Why is mockMvc and the other classes I #Autowired null?
I need a way to spin up the server, do some common setup and run the tests in parallel.

Categories