I have to store "extractedNumbers" into SQL Database but I can't manage it. I have created ExtractedNumbersRepository interface but I can't access it from this method and I don't know how to do it. Could somebody help me please?
public class Lotto6From49 extends Thread {
// Save lucky numbers in Database
public ExtractedNumbers addExNr() {
ExtractedNumbers extractedNumbers = new ExtractedNumbers();
extractedNumbers.setExtractedNumbers(luckyNumbers.toString());
extractedNumbers.setExtractionDate(timeNow);
return extractedNumbers;
}
}
#Repository
public interface ExtractedNumbersRepository extends JpaRepository<ExtractedNumbers,Integer> {
}
You should implement a service and inject the ExtractedNumbersRepository dependency:
#Service
public class ExtractedNumbersServiceImpl {
#Autowired
ExtractedNumbersRepository theExtractedNumbersRepository;
public void saveextractedNumbers(ExtractedNumbers extractedNumbers){
//here you can call the repository and save the extracted numbers
this.theExtractedNumbersRepository.save(extractedNumbers);
}
}
The next step is to inject the Service dependency inside your Lotto6From49 or controller:
private final ExtractedNumbersServiceImpl theExtractedNumbersServiceImpl;
public Lotto6From49(ExtractedNumbersServiceImpl extractedNumbersServiceImpl) {
this.theExtractedNumbersServiceImpl= extractedNumbersServiceImpl;
}
Now you can call:
theExtractedNumbersServiceImpl.saveextractedNumbers(extractedNumbers);
Related
I have two unit tests that fails because of "[RuntimeException: Missing CSRF Token]":
running(testServer(3333, provideFakeApplication()), () -> {
assertThat(WS.url("http://localhost:3333").get().get(3000).getStatus()
).isEqualTo(OK);
and
running(testServer(3333, provideFakeApplication()), HTMLUNIT, browser -> {
browser.goTo("http://localhost:3333");
assert....
How can I add a session with a CSRF token to the WS.url and the browser.goTo?
The tests are trying to reach a page that has a form.
A global solution would be to use a fake application that has the CSRF filter enabled. To do that you need to modify (i.e. create a class that inherits from WithApplication and override) your provideFakeApplication() such as it creates the fake application passing in the global settings:
public abstract class TestWrapper extends WithApplication {
public class GlobalTestSettings extends play.GlobalSettings {
#Override
public <T extends EssentialFilter> Class<T>[] filters() {
return new Class[] { CSRFFilter.class };
}
}
#Override
protected FakeApplication provideFakeApplication() {
stop(fakeApplication()); // Stop the existing fake app and start over
Map<String, String> addConfig = new HashMap<>();
return fakeApplication(addConfig, new GlobalTestSettings());
}
}
I am new to unit testing with spring-test. I have a spring-mvc-rest application. I am able to mock the environment using MockMvc.
My question is do I need to build the MockMvc object in every class of testing?
Would that not be repetitive configuration.
Is there a way to define this in one class and use it across every testing class?
If we go by single configuration or multiple configuration then which is the best way (by design and by maintenance)?
It's called inhertance
for example
Base Class
#ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
public class BaseTest
{
protected MockMvc mockMvc;
#Before
public void setup()
{
mockMvc = MockMvcBuilders.standaloneSetup().build();
}
}
Extend
public class ExtendedTest extends BaseTest
{
#Test
public void test()
{
//execute test here we have access to the mockMVC
}
}
So basically I'm trying to write a Junit using powermockito for a adapter for a service class which consumes a webservice.
I have an adapter with a constructor that inturn creates a new service object in it's own constructor by calling a super class. I have to test my adapter. I have used power mockito to mock my adapter as well as my service class but I don't think the mocked object is able to perform the super call. The following is the structure of my code. I want the super class to return my mocked object upon call.
public class CommonPoolingServiceAdp {
private CPSSecurity cpsServicePort;
public CommonPoolingServiceAdp() {
CommonPoolingService service= new CommonPoolingService();
cpsServicePort=service.getCommonPoolingServicePort();
}
public SercurityDataResponse getBroadcastElements(broadcastReqObj)
{
SercurityDataResponse=null;
response=cpsServicePort.getBroadcastElements(broadcaseRequestObj);
}
}
public class CommonPoolingService extends Service {
{
static
{
//few mandatory initializations
}
public CommonPoolingService()
{
super(WSDL_Location,QName);
}
public CSPSecurity getCommonPoolingServicePort() {
return super.getPort(QName);
}
}
}
Please share a bit more of your code. By the way, this is how you mock a super class method :
public class SuperClass {
public void method() {
methodA(); // I don't want to run this!
}
}
public class MyClass extends SuperClass{
public void method(){
super.method()
methodB(); // I only want to test this!
}
}
#Test
public void testMethod() {
MyClass spy = Mockito.spy(new MyClass());
// Prevent/stub logic in super.method()
Mockito.doNothing().when((SuperClass)spy).methodA();
// When
spy.method();
// Then
verify(spy).methodB();
}
Hope, it will help.
I have an android application project. I have created a library project and added reference in the application project. Now I need to call/access certain functions/class/methods that is there in the application project from the library project. How can I do that ?
Create an interface in the library that defines the functions you would like the library to call. Have the application implement the interface and then register the implementing object with the library. Then the library can call the application through that object.
In the library, declare the interface and add the registration function:
public class MyLibrary {
public interface AppInterface {
public void myFunction();
}
static AppInterface myapp = null;
static void registerApp(AppInterface appinterface) {
myapp = appinterface;
}
}
Then in your application:
public class MyApplication implements MyLibrary.AppInterface {
public void myFunction() {
// the library will be able to call this function
}
MyApplication() {
MyLibrary.registerApp(this);
}
}
You library can now call the app through the AppInterface object:
// in some library function
if (myapp != null) myapp.myFunction();
You can just create the object of that particular class and then you can directly call that method or variable.
class A{
public void methodA(){
new B().methodB();
//or
B.methodB1();
}
}
class B{
//instance method
public void methodB(){
}
//static method
public static void methodB1(){
}
}
Don't forget to import the necessary packages.
I am using PowerMocking for JUNIT and Iam new to PowerMock.
I want to mock one class which is non static.
The class scenario goes as follows.
public class Export extends MyUtil implements ExportFormatting<DeptSummaryByDCDTO, LmReportsInputDTO>{
public String createPDF(List<DeptSummaryByDCDTO> summaryDtoList, LmReportsInputDTO inputDto){
}
public String createPDF(Map<String, DeptSummaryByDCDTO> paramMap,
LmReportsInputDTO paramK) {
}
}
The calling class is as follows.
public static Response getMultiplePackSku{
filePath = new Export(inputDto).createPDF(resultList,null);
}
The Question is,
I am trying to test the above class using powermock.
Can anybody tell how to mock the line filePath.....
You need to first mock the constructor and return an Export mock. On the returned mock you need to record the call to createPDF. The tricky part is the constructor mocking. I'll give you an example, hopefully you'll get all of it:
#RunWith(PowerMockRunner.class) // This annotation is for using PowerMock
#PrepareForTest(Export.class) // This annotation is for mocking the Export constructor
public class MyTests {
private mockExport;
#Before
public void setUp () {
// Create the mock
mockExport = PowerMock.createMock(Export.class)
}
#Test
public void testWithConstructor() {
SomeDtoClass inputDto = PowerMock.createMock(SomeDtoClass.class);
PowerMock.expectNew(Export.class, inputDto).andReturn(mockExport);
PowerMock.replay(mockExport, Export.class);
expect(mockExport.createPDF(resultList, null);
// Run the tested method.
}
}
Here is a description of how to mock a constructor call: MockConstructor