How to mock interceptors? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 days ago.
The community is reviewing whether to reopen this question as of 13 days ago.
Improve this question
How to mock interceptor methods during test?
Example of class with interceptor:
#ApplicationScoped
public class MyServiceImpl implements MyService {
#Override
#MyAnnotation
public Uni<List<SomeDto>> getAll() {
return //
}
Example of interceptor which works via #MyAnnotation:
#Priority(100)
#MyAnnotation
#Interceptor
public class MyInterceptor {
#AroundInvoke
public Object someMethod(InvocationContext ic)throws Exception {
// some logic
return ic.proceed();
}
}
How test should be designed to mock invocation of someMethod in MyInterceptor?
#QuarkusTest
class SomeServiceTest {
#Test
void getAllNotBlacklisted() {
// ???
}

Related

Should I write unit test for void methods (includes repository call)? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I have the following code snippets in my spring boot application:
#Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
public UserServiceImpl(UserRepository ...) {...}
#Override
public void saveOrUpdateUser(UserDAO user) {
userRepository.save(user);
}
}
Test case class:
class UserServiceImplUnitTest {
private UserRepository userRepository = mock(UserRepository.class);
private UserService userService;
#BeforeEach
void setup() {
userService = new UserServiceImpl(userRepository);
}
}
I am using mockito to write my test cases. However, for this void method should i write test case(s)? If yes, how can i write it?
Yes. You should mainly focus on whether UserServiceImpl interacts with UserRepository in an expected way such as the things like if it invokes the correct method on the UserRepository with the correct arguments etc.
By using Mockito to mock the UserRepository , you may end up with a test something like as follows :
#ExtendWith(MockitoExtension.class)
public class UserServiceImplUnitTest {
#Mock
UserRepository userRepository ;
UserService userService;
#BeforeEach
void setup() {
userService = new UserServiceImpl(userRepository);
}
#Test
public void testSaveOrUpdateUser(){
UserDAO user = createDummyUser();
userService.saveOrUpdateUser(user);
verify(userRepository).save(same(user));
}
}

Prioritizing execution order of JUNIT test classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have two individual test classes ( for example : TestClassA, TestClassB) I want TestClassA to run first and TestClassB to run next. How can i do so. Any ideas.
Note that i am using Springboot here and my test dependency is : spring-boot-starter-test
you can do using junit 5
#Test
#Order(1)
public void firstTest() {
output.append("a");
}
#Test
#Order(2)
public void secondTest() {
output.append("b");
}
check refence here https://www.baeldung.com/junit-5-test-order
and for different classes use test suite
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({ TestClass1.class, TestClass2.class })
public class AllTests {
}
As mentioned in the comments, ordering tests or having tests dependent on each other is an anti pattern and make your testing strategy brittle. So ordering should be handled with care.
With Junit 4 you can order your test
#TestMethodOrder(OrderAnnotation.class)
public class OrderedTest {
#Test
#Order(1)
public void firstTest() {
}
#Test
#Order(2)
public void secondTest() {
}
#Test
#Order(3)
public void thirdTest() {
}
}
With Junit 5
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MethodOrderTest {
#Test
#Order(3)
void test1() {
}
#Test
#Order(1)
void test2() {
}
#Test
#Order(2)
void test3() {
}
}

RestTemplate Call with No Proxy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have set proxy in my environment variables , however for a specific call made via RestTemplate , I shouldn't be using any proxy. Is there a way to disable proxy for a specific resttemplate call?
Instead of disabling and re-enabling the proxy, I'd use separate RestTemplates:
#Configuration
public class SomeConfig {
#Bean(name="proxyRestTemplate")
public RestTemplate proxyRestTemplate() {
// return a RestTemplate with proxy settings
}
#Bean(name="nonProxyRestTemplate")
public RestTemplate nonProxyRestTemplate() {
// return a RestTemplate without proxy settings
}
}
And in your classes:
#Component
public class SomeClassWithProxy {
private final RestTemplate restTemplate;
public SomeClass(#Qualifier("proxyRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}
#Component
public class SomeClassWithoutProxy {
private final RestTemplate restTemplate;
public SomeClass(#Qualifier("nonProxyRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}

How to implement in testng junits #ClassRule using AfterXXX and BeforeXXX [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
As I googled, this question have accepted answer saying, that there is a way to implement the #ClassRule by using AfterXXX and BeforeXXX methods.
How to implement #ClassRule using those methods?
JUnit:
public class UsesExternalResource {
public static Server myServer= new Server();
#ClassRule
public static ExternalResource resource= new ExternalResource() {
#Override
protected void before() throws Throwable {
myServer.connect();
};
#Override
protected void after() {
myServer.disconnect();
};
};
}
TestNG:
public class UsesExternalResource {
public Server myServer= new Server();
#BeforeClass
public void before() {
myServer.connect();
}
#AfterClass
public void before() {
myServer.disconnect();
}
}

JUnit create dao fake object JAVA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to test a class which is using dao. In my test class I have mocked the DAO and inject the mock into an instance of the class that I test. I am trying to make a fake object of the DAO class.
#RunWith(MockitoJUnitRunner.class)
public class UserManagerTest {
#Mock
private UserManagerDao umDao;
#InjectMocks
private UserManager um = new UserManager();
#Before
public void initializeMockito() {
MockitoAnnotations.initMocks(this);
}
public void testGetUserId() {
}
This here are the methods from UserManager.class and Dao class
userManager.class
public long getUserId(String email) throws Exception {
String[] partsOfMail = email.split("#");
return umDao.getUserId(partsOfMail[0], partsOfMail[1]);
}
Dao class
public long getUserId(String userName, String domain) throws Exception {
String sql = msa.getMessage("sql.select.user_id");
Object[] params = new Object[] { userName, domain };
List<Long> result = getJdbcTemplate().queryForList(sql, params, Long.class);
if (result.size() > 0) {
return result.get(0);
}
return 0;
}
See comments:
#RunWith(MockitoJUnitRunner.class)
public class UserManagerTest {
#Mock
private UserManagerDao umDao; // a mock object should present here
/* #InjectMocks should be able to create an instance
with mocks injected for you - you don't need to create it
yourself */
#InjectMocks
private UserManager um; // = new UserManager();
/* Not required as you're using the MockitoJUnitRunner
#Before
public void initializeMockito() {
MockitoAnnotations.initMocks(this);
} */
// Add test annotation (assuming JUnit4 as you're not extending TestCase)
#Test
public void testGetUserId() {
// Both these fields should not be null
Assert.assertNotNull(umDao);
Assert.assertNotNull(um);
}
For more information take a look at MockitoJUnitRunner and InjectMocks documentation.
Mockito's documentation, which included many examples, can be found here.
What is your question? If you are asking how to mock some method in the DAO, this is a simple example.
#Test
public void testGetUserId() {
when(umDao.getUserId(<PARS>)).thenReturn(1L);
assertThat(um.getUserId(<PAR>)).isEqualTo(1L);
}

Categories

Resources